-
Notifications
You must be signed in to change notification settings - Fork 1
Implement auto-fix for markdown flavor validation rule #169
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
3 changes: 3 additions & 0 deletions
3
internal/rules/MDS034-markdown-flavor/fixed/commonmark-bare-url.md
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,3 @@ | ||
| # Heading | ||
|
|
||
| Visit <https://example.com> for details. |
3 changes: 3 additions & 0 deletions
3
internal/rules/MDS034-markdown-flavor/fixed/commonmark-heading-id.md
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,3 @@ | ||
| # Heading | ||
|
|
||
| Body text. |
3 changes: 3 additions & 0 deletions
3
internal/rules/MDS034-markdown-flavor/fixed/commonmark-strikethrough.md
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,3 @@ | ||
| # Heading | ||
|
|
||
| Text crossed out here. |
3 changes: 3 additions & 0 deletions
3
internal/rules/MDS034-markdown-flavor/fixed/commonmark-subscript.md
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,3 @@ | ||
| # Heading | ||
|
|
||
| H2O is water. |
3 changes: 3 additions & 0 deletions
3
internal/rules/MDS034-markdown-flavor/fixed/commonmark-superscript.md
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,3 @@ | ||
| # Heading | ||
|
|
||
| E = mc2 is famous. |
4 changes: 4 additions & 0 deletions
4
internal/rules/MDS034-markdown-flavor/fixed/commonmark-task-list.md
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,4 @@ | ||
| # Heading | ||
|
|
||
| - done | ||
| - todo |
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,184 @@ | ||
| package markdownflavor | ||
|
|
||
| import ( | ||
| "sort" | ||
|
|
||
| "github.com/yuin/goldmark/ast" | ||
| extast "github.com/yuin/goldmark/extension/ast" | ||
| "github.com/yuin/goldmark/text" | ||
|
|
||
| "github.com/jeduden/mdsmith/internal/lint" | ||
| "github.com/jeduden/mdsmith/internal/rules/markdownflavor/ext" | ||
| ) | ||
|
|
||
| // edit describes a single byte-range substitution to apply to source. | ||
| // applyEdits assumes non-overlapping spans and rewrites the buffer in | ||
| // one pass. | ||
| type edit struct { | ||
| start, end int | ||
| repl []byte | ||
| } | ||
|
|
||
| // fixByteRangeFeatures collects edits for the six byte-range features | ||
| // (heading IDs, strikethrough, task lists, superscript, subscript, and | ||
| // bare-URL autolinks) and returns the rewritten source. Features that | ||
| // the configured flavor accepts are skipped. The function returns the | ||
| // source unchanged when no edit applies. | ||
| func (r *Rule) fixByteRangeFeatures(f *lint.File) []byte { | ||
| var edits []edit | ||
|
|
||
| if r.needsAnyDualFix() { | ||
| doc := Parser().Parser().Parse(text.NewReader(f.Source)) | ||
| _ = ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) { | ||
| if !entering { | ||
| return ast.WalkContinue, nil | ||
| } | ||
| edits = append(edits, r.dualNodeEdits(f, n)...) | ||
| return ast.WalkContinue, nil | ||
| }) | ||
| } | ||
|
|
||
| if !r.Flavor.Supports(FeatureBareURLAutolinks) { | ||
| for _, fin := range detectBareURLs(f) { | ||
| edits = append(edits, wrapBareURL(f.Source, fin)) | ||
| } | ||
| } | ||
|
|
||
| if len(edits) == 0 { | ||
| return f.Source | ||
| } | ||
| return applyEdits(f.Source, edits) | ||
| } | ||
|
|
||
| // needsAnyDualFix reports whether any dual-parser fixable feature is | ||
| // unsupported by the configured flavor. Skips the dual re-parse for | ||
| // flavors that accept every dual feature (e.g. FlavorAny, Pandoc). | ||
| func (r *Rule) needsAnyDualFix() bool { | ||
| for _, feat := range []Feature{ | ||
| FeatureHeadingIDs, FeatureStrikethrough, FeatureTaskLists, | ||
| FeatureSuperscript, FeatureSubscript, | ||
| } { | ||
| if !r.Flavor.Supports(feat) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // dualNodeEdits returns the edits to remove an unsupported feature | ||
| // produced from a dual-parser AST node. Returns nil when the node is | ||
| // either supported or not a fixable feature. | ||
| func (r *Rule) dualNodeEdits(f *lint.File, n ast.Node) []edit { | ||
| switch node := n.(type) { | ||
| case *ast.Heading: | ||
| if r.Flavor.Supports(FeatureHeadingIDs) { | ||
| return nil | ||
| } | ||
| return headingIDEdits(f, node) | ||
| case *extast.Strikethrough: | ||
| if r.Flavor.Supports(FeatureStrikethrough) { | ||
| return nil | ||
| } | ||
| return delimiterPairEdits(node, len("~~")) | ||
| case *extast.TaskCheckBox: | ||
| if r.Flavor.Supports(FeatureTaskLists) { | ||
| return nil | ||
| } | ||
| return taskCheckBoxEdits(f, node) | ||
| case *ext.SuperscriptNode: | ||
| if r.Flavor.Supports(FeatureSuperscript) { | ||
| return nil | ||
| } | ||
| return delimiterPairEdits(node, len("^")) | ||
| case *ext.SubscriptNode: | ||
| if r.Flavor.Supports(FeatureSubscript) { | ||
| return nil | ||
| } | ||
| return delimiterPairEdits(node, len("~")) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // headingIDEdits returns the edit that drops a "{#id}" attribute block | ||
| // plus any whitespace separating it from the heading text. Returns nil | ||
| // when the heading carries no id attribute. findHeadingID always stores | ||
| // a HeadingIDExtra in fin.Extra, so the type assertion is unchecked. | ||
| func headingIDEdits(f *lint.File, h *ast.Heading) []edit { | ||
| fin, ok := findHeadingID(f, h) | ||
| if !ok { | ||
| return nil | ||
| } | ||
| hx := fin.Extra.(HeadingIDExtra) | ||
| start := hx.AttrStart | ||
| for start > 0 && (f.Source[start-1] == ' ' || f.Source[start-1] == '\t') { | ||
| start-- | ||
| } | ||
| return []edit{{start: start, end: hx.AttrEnd}} | ||
| } | ||
|
|
||
| // delimiterPairEdits returns edits removing the opening and closing | ||
| // delimiter runs that wrap an inline node. Only nodes with a single | ||
| // Text child are fixed; goldmark merges adjacent text inlines so a | ||
| // second sibling (whether Text-typed or not) implies nested inline | ||
| // markup like `~~*bold*~~` or a softbreak inside the wrapper, where | ||
| // reconstructing each child's own marker span is brittle. The fix | ||
| // declines and the diagnostic remains for the user to resolve. | ||
| func delimiterPairEdits(n ast.Node, markerLen int) []edit { | ||
| t, ok := n.FirstChild().(*ast.Text) | ||
| if !ok || t.NextSibling() != nil { | ||
| return nil | ||
| } | ||
| return []edit{ | ||
| {start: t.Segment.Start - markerLen, end: t.Segment.Start}, | ||
| {start: t.Segment.Stop, end: t.Segment.Stop + markerLen}, | ||
| } | ||
| } | ||
|
|
||
| // taskCheckBoxEdits removes the "[X]" run plus a single trailing | ||
| // space when present. Per the plan, the bullet itself is preserved. | ||
| // The dual parser places every TaskCheckBox at the start of a | ||
| // TextBlock so block.Lines().At(0).Start always points at '['. | ||
| func taskCheckBoxEdits(f *lint.File, n *extast.TaskCheckBox) []edit { | ||
| block := nearestBlockAncestor(n) | ||
| start := block.Lines().At(0).Start | ||
| end := start + 3 | ||
| if end < len(f.Source) && f.Source[end] == ' ' { | ||
| end++ | ||
| } | ||
| return []edit{{start: start, end: end}} | ||
| } | ||
|
|
||
| // wrapBareURL wraps a bare URL in angle brackets so the renderer treats | ||
| // it as a CommonMark autolink. The detector reports a precise span via | ||
| // fin.Start / fin.End. | ||
| func wrapBareURL(source []byte, fin Finding) edit { | ||
| url := source[fin.Start:fin.End] | ||
| repl := make([]byte, 0, len(url)+2) | ||
| repl = append(repl, '<') | ||
| repl = append(repl, url...) | ||
| repl = append(repl, '>') | ||
| return edit{start: fin.Start, end: fin.End, repl: repl} | ||
| } | ||
|
|
||
| // applyEdits rewrites src by appending unchanged spans and replacement | ||
| // bytes in a single pass. Edits are sorted by ascending start offset; | ||
| // the detection layer never produces overlapping edits for the | ||
| // features we fix, so applyEdits assumes non-overlapping spans. | ||
| func applyEdits(src []byte, edits []edit) []byte { | ||
|
jeduden marked this conversation as resolved.
|
||
| sort.SliceStable(edits, func(i, j int) bool { | ||
| return edits[i].start < edits[j].start | ||
| }) | ||
| size := len(src) | ||
| for _, e := range edits { | ||
| size += len(e.repl) - (e.end - e.start) | ||
| } | ||
| out := make([]byte, 0, size) | ||
| cursor := 0 | ||
| for _, e := range edits { | ||
| out = append(out, src[cursor:e.start]...) | ||
| out = append(out, e.repl...) | ||
| cursor = e.end | ||
| } | ||
| out = append(out, src[cursor:]...) | ||
| return out | ||
| } | ||
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.