-
Notifications
You must be signed in to change notification settings - Fork 0
[Feature] tree sitter markdown scan #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,9 @@ | ||
| # Literate Documentation (`litdoc`) | ||
|
|
||
| ### Development | ||
|
|
||
| To test, run: | ||
|
|
||
| ```bash | ||
| nix develop --ignore-environment --command make test | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,27 @@ | ||
| github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= | ||
| github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
| github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= | ||
| github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= | ||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ= | ||
| github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc= | ||
| github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
| github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82 h1:6C8qej6f1bStuePVkLSFxoU22XBS165D3klxlzRg8F4= | ||
| github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82/go.mod h1:xe4pgH49k4SsmkQq5OT8abwhWmnzkhpgnXeekbx2efw= | ||
| github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= | ||
| github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4= | ||
| github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY= | ||
| github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= | ||
| github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= | ||
| github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= | ||
| go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= | ||
| golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= | ||
| golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
| golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ= | ||
| golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
| gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
|
|
||
| sitter "github.com/smacker/go-tree-sitter" | ||
| "github.com/smacker/go-tree-sitter/markdown" | ||
| ) | ||
|
|
||
| type BlockKind string | ||
|
|
||
| const ( | ||
| BlockKindText BlockKind = "Text" | ||
| BlockKindFencedCode BlockKind = "fencedCode" | ||
| BlockKindHTMLComment BlockKind = "HTMLComment" | ||
| ) | ||
|
|
||
| type Block struct { | ||
| kind BlockKind | ||
| content []byte | ||
| } | ||
|
|
||
| func MakeBlockFromRaw(kind BlockKind, raw []byte) Block { | ||
| return Block{kind: kind, content: raw} | ||
| } | ||
|
|
||
| func (b Block) Kind() BlockKind { return b.kind } | ||
|
|
||
| func (b Block) Content() []byte { return b.content } | ||
|
|
||
| func MakeBlocksFromMarkdown(content []byte) ([]Block, error) { | ||
| tree, err := markdown.ParseCtx(context.Background(), nil, content) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("parsing markdown: %w", err) | ||
| } | ||
|
|
||
| root := tree.BlockTree().RootNode() | ||
| var blocks []Block | ||
| pos := uint32(0) | ||
|
|
||
| collectBlockNodes(root, content, &pos, &blocks) | ||
|
|
||
| if pos < uint32(len(content)) { | ||
| blocks = append(blocks, Block{kind: BlockKindText, content: content[pos:]}) | ||
| } | ||
|
|
||
| return blocks, nil | ||
| } | ||
|
|
||
| func collectBlockNodes( | ||
|
mwittie marked this conversation as resolved.
|
||
| node *sitter.Node, | ||
| content []byte, | ||
| pos *uint32, | ||
| blocks *[]Block, | ||
| ) { | ||
| switch node.Type() { | ||
| case "document", "section": | ||
| for i := 0; i < int(node.ChildCount()); i++ { | ||
| collectBlockNodes(node.Child(i), content, pos, blocks) | ||
| } | ||
| default: | ||
| start := node.StartByte() | ||
| end := node.EndByte() | ||
|
|
||
| if start > *pos { | ||
| *blocks = append( | ||
| *blocks, | ||
| MakeBlockFromRaw(BlockKindText, content[*pos:start]), | ||
| ) | ||
| } | ||
|
|
||
| *blocks = append( | ||
| *blocks, | ||
| MakeBlockFromRaw(blockKind(node, content), content[start:end]), | ||
| ) | ||
| *pos = end | ||
| } | ||
| } | ||
|
|
||
| func blockKind(node *sitter.Node, content []byte) BlockKind { | ||
| switch node.Type() { | ||
| case "fenced_code_block": | ||
| return BlockKindFencedCode | ||
| case "html_block": | ||
| if bytes.HasPrefix(content[node.StartByte():node.EndByte()], []byte("<!--")) { | ||
| return BlockKindHTMLComment | ||
| } | ||
| return BlockKindText | ||
| default: | ||
| return BlockKindText | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| package internal_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "litdoc/internal" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestMakeBlockFromRaw(t *testing.T) { | ||
| // given | ||
| kind := internal.BlockKindFencedCode | ||
| content := []byte("```bash\necho hello\n```\n") | ||
|
|
||
| // when | ||
| got := internal.MakeBlockFromRaw(kind, content) | ||
|
|
||
| // then | ||
| assert.Equal(t, kind, got.Kind()) | ||
| assert.Equal(t, content, got.Content()) | ||
| } | ||
|
|
||
| func TestMakeBlocksFromMarkdown(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| want []struct { | ||
| kind internal.BlockKind | ||
| content string | ||
| } | ||
| }{ | ||
| { | ||
| name: "heading", | ||
| input: "# Hello\n", | ||
| want: []struct { | ||
| kind internal.BlockKind | ||
| content string | ||
| }{ | ||
| {internal.BlockKindText, "# Hello\n"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "text", | ||
| input: "text\nand more text", | ||
| want: []struct { | ||
| kind internal.BlockKind | ||
| content string | ||
| }{ | ||
| {internal.BlockKindText, "text\nand more text"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "fenced code block", | ||
| input: "```bash\necho \"hello\"\n```\n", | ||
| want: []struct { | ||
| kind internal.BlockKind | ||
| content string | ||
| }{ | ||
| {internal.BlockKindFencedCode, "```bash\necho \"hello\"\n```\n"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "tilde fenced code block", | ||
| input: "~~~bash\necho \"hello\"\n~~~\n", | ||
| want: []struct { | ||
| kind internal.BlockKind | ||
| content string | ||
| }{ | ||
| {internal.BlockKindFencedCode, "~~~bash\necho \"hello\"\n~~~\n"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "longer backtick fence", | ||
| input: "````bash\necho \"hello\"\n````\n", | ||
| want: []struct { | ||
| kind internal.BlockKind | ||
| content string | ||
| }{ | ||
| {internal.BlockKindFencedCode, "````bash\necho \"hello\"\n````\n"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "quad fenced verbatim block containing litdoc source", | ||
| input: "````md\n```bash | litdoc\necho \"hello\"\n```\n````", | ||
| want: []struct { | ||
| kind internal.BlockKind | ||
| content string | ||
| }{ | ||
| { | ||
| internal.BlockKindFencedCode, | ||
| "````md\n```bash | litdoc\necho \"hello\"\n```\n````", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "indented code block", | ||
| input: " echo \"hello\"\n", | ||
| want: []struct { | ||
| kind internal.BlockKind | ||
| content string | ||
| }{ | ||
| {internal.BlockKindText, " echo \"hello\"\n"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "html comment block", | ||
| input: "<!--\ncomment\n-->\n", | ||
| want: []struct { | ||
| kind internal.BlockKind | ||
| content string | ||
| }{ | ||
| {internal.BlockKindHTMLComment, "<!--\ncomment\n-->\n"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "html block is not a comment", | ||
| input: "<div>\nhello\n</div>\n", | ||
| want: []struct { | ||
| kind internal.BlockKind | ||
| content string | ||
| }{ | ||
| {internal.BlockKindText, "<div>\nhello\n</div>\n"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "mixed content", | ||
| input: "# Title\n\n```go\nfmt.Println()\n```\n\n<!--\nnote\n-->\n", | ||
| want: []struct { | ||
| kind internal.BlockKind | ||
| content string | ||
| }{ | ||
| {internal.BlockKindText, "# Title\n"}, | ||
| {internal.BlockKindText, "\n"}, | ||
| {internal.BlockKindFencedCode, "```go\nfmt.Println()\n```\n"}, | ||
| {internal.BlockKindText, "\n"}, | ||
| {internal.BlockKindHTMLComment, "<!--\nnote\n-->\n"}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // when | ||
| blocks, err := internal.MakeBlocksFromMarkdown([]byte(tt.input)) | ||
|
|
||
| // then | ||
| require.NoError(t, err) | ||
| require.Len(t, blocks, len(tt.want)) | ||
| for i, w := range tt.want { | ||
| assert.Equal( | ||
| t, | ||
| w.kind, | ||
| blocks[i].Kind(), | ||
| "block[%d] kind", | ||
| i, | ||
| ) | ||
| assert.Equal( | ||
| t, | ||
| w.content, | ||
| string(blocks[i].Content()), | ||
| "block[%d] content", | ||
| i, | ||
| ) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.