-
-
Notifications
You must be signed in to change notification settings - Fork 25
fix: flip attention marker to avoid nested-emphasis fusion #74
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
Open
ChristianMurphy
wants to merge
1
commit into
main
Choose a base branch
from
fix/nested-attention-marker-flip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /** | ||
| * @import {Emphasis, Parents} from 'mdast' | ||
| * @import {State} from 'mdast-util-to-markdown' | ||
| */ | ||
|
|
||
| import {checkEmphasis} from './check-emphasis.js' | ||
|
|
||
| /** | ||
| * Pick the marker to use for an emphasis node, flipping from the configured | ||
| * marker to its opposite when the configured marker would fuse with an | ||
| * adjacent attention delimiter and re-parse as a different construct. | ||
| * | ||
| * Only emphasis gets the flip. Strong already round-trips through the | ||
| * spec's attention algorithm because a run of 4 asterisks pairs as two | ||
| * strong delimiters, and a run of 6 as three, and so on. Nested emphasis | ||
| * is the asymmetric case: a run of 2 asterisks pairs as one strong, not as | ||
| * two nested emphases, so without a flip `emphasis > emphasis > text` | ||
| * round-trips as `strong > text`. | ||
| * | ||
| * Two situations drive a flip, both narrowly scoped to avoid disturbing | ||
| * shapes the serializer already handles via fusion: | ||
| * | ||
| * 1. The emphasis is an only child of an attention parent (emphasis or | ||
| * strong), and both its opening and closing markers would be adjacent | ||
| * to the parent's primary marker. Using the opposite marker (for | ||
| * example, `*_a_*` for `emphasis > emphasis > text` with primary | ||
| * `*`) breaks the fusion. | ||
| * | ||
| * 2. The emphasis sits at the top of a strict same-type chain of depth at | ||
| * least 2 (each link has exactly one emphasis child), with primary | ||
| * `*`. Three-deep emphasis collapses under rule 17 unless the | ||
| * outermost marker is `_`, because `_`'s flanking rules are stricter | ||
| * than `*`'s. The check is asymmetric by design: when the configured | ||
| * marker is already `_`, the adjacency flip in rule 1 alone is enough. | ||
| * | ||
| * @param {Emphasis} node | ||
| * @param {Parents | undefined} parent | ||
| * @param {State} state | ||
| * @param {{before: string, after: string}} info | ||
| * Only the `before` and `after` fields are read. | ||
| * @returns {'*' | '_'} | ||
| */ | ||
| export function emphasisMarker(node, parent, state, info) { | ||
| const primary = checkEmphasis(state) | ||
| const other = primary === '*' ? '_' : '*' | ||
|
|
||
| if ( | ||
| parent && | ||
| (parent.type === 'emphasis' || parent.type === 'strong') && | ||
| 'children' in parent && | ||
| parent.children.length === 1 && | ||
| info.before.charAt(info.before.length - 1) === primary && | ||
| info.after.charAt(0) === primary | ||
| ) { | ||
| return other | ||
| } | ||
|
|
||
| if (primary === '*' && strictChainDepth(node) >= 2) return other | ||
|
|
||
| return primary | ||
| } | ||
|
|
||
| /** | ||
| * Count the depth of a strict single-child emphasis chain descending from | ||
| * `node`. A chain is strict when every link has exactly one child and that | ||
| * child is also `emphasis`. | ||
| * | ||
| * @param {Emphasis} node | ||
| * @returns {number} | ||
| */ | ||
| function strictChainDepth(node) { | ||
| const children = node.children | ||
| if (!children || children.length !== 1) return 0 | ||
| const only = children[0] | ||
| if (only.type !== 'emphasis') return 0 | ||
| return 1 + strictChainDepth(only) | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
observation is correct:
strictChainDepth(node) >= 2does match multiple nodes in a strict chain of depth 4 or deeper, which I'll grant wasn't by explicit design. But applying the proposed gating is a regression, not an improvement. Comparison across depths:*_a_*em > em > text("a")✅*_a_*em > em > text("a")✅_*_a_*_em > em > em > text("a")✅_*_a_*___*_a_*__strong > em > em > text("a")_*_*a*_*_[em(text("*")), em("a"), em(text("*"))]___*_a_*___em > strong > em > em > text("a")_*_*_a_*_*_emwith literal*text____*_a_*____strong > strong > em > em > text("a")_*_*_*a*_*_*_emwith literal*textBoth approaches drift at depth 4+, CommonMark has no round-tripping representation for a strict 4-deep emphasis chain. But the current output is strictly more faithful to the input tree:
"a") is preserved.em > emsurvives).The proposed fix:
emnodes.*or_characters into text nodes that weren't in the original tree (content corruption).So the
__...__at the outer boundary looks alarming, but it's producing the least-bad degradation at depth 4+. I'd rather keep the accidental over-match and document it than trade it for a drift that corrupts content.