Repository navigation
fix(stringify): keep lists, nested emphasis and multi-line headings on round trip #604
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
9 commits
Select commit
Hold shift + click to select a range
178f1c9
fix(stringify): keep lists, nested emphasis and multi-line headings o…
aliassar f5c7427
Merge branch 'main' into fix-stringify-roundtrip
aliassar 11da380
fix(stringify): pick list markers in one pass per sibling collection
aliassar 0bcea8a
fix(stringify): keep `*` for emphasis that does not touch its parent's
aliassar d794f31
fix(stringify): escape the first line of a multi-line heading
aliassar 1186ba9
fix(stringify): separate a nested list that starts past 1 from the it…
aliassar 32bea0e
test: update bundle size snapshot
aliassar f869836
Merge branch 'main' into fix-stringify-roundtrip
aliassar 21e560e
fix(stringify): only alternate emphasis marker directly inside emphasis
farnabaz 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
16 changes: 14 additions & 2 deletions
16
packages/comark/src/internal/stringify/handlers/emphesis.ts
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,17 +1,29 @@ | ||
| import type { State } from 'comark/render' | ||
| import type { ElementNode } from 'comark' | ||
| import { comarkAttributes } from '../attributes.ts' | ||
| import { sourceNode } from '../state.ts' | ||
|
|
||
| export async function emphesis(node: ElementNode, state: State) { | ||
| export async function emphesis(node: ElementNode, state: State, parent?: ElementNode) { | ||
| const [_, attrs, ...children] = node | ||
|
|
||
| // Emphasis that opens or closes its parent emphasis alternates `*` and `_`: | ||
| // `**foo**` would read back as strong. Elsewhere it keeps `*`, since `_` | ||
| // cannot open or close inside a word (`*foo *bar*baz*`, `_x [a*b*](/u) y_`). | ||
| const source = sourceNode(node) | ||
| const atEdge = | ||
| parent !== undefined && parent[0] === 'em' && (parent[2] === source || parent[parent.length - 1] === source) | ||
| const marker = state.context.emphasisMarker === '*' && atEdge ? '_' : '*' | ||
| const revert = state.applyContext({ emphasisMarker: marker }) | ||
|
|
||
| let content = '' | ||
| for (const child of children) { | ||
| content += await state.one(child, state, node) | ||
| } | ||
| content = content.trim() | ||
|
|
||
| state.applyContext(revert) | ||
|
|
||
| const attrsString = Object.keys(attrs).length > 0 ? comarkAttributes(attrs) : '' | ||
|
|
||
| return `*${content}*${attrsString}` | ||
| return `${marker}${content}${marker}${attrsString}` | ||
| } |
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
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,51 @@ | ||
| import type { State } from 'comark/render' | ||
| import type { ElementNode, MarkdownDocument } from 'comark' | ||
| import { comarkAttributes, userBlockAttrs } from './attributes.ts' | ||
| import { sourceNode } from './state.ts' | ||
|
|
||
| // Per render and per sibling collection, the lists that take the second marker. | ||
| const alternateLists = new WeakMap<State['context'], WeakMap<readonly unknown[], Set<unknown>>>() | ||
|
|
||
| /** | ||
| * Pick the marker of a `ul` (`-` or `*`) or `ol` (`.` or `)`) node. | ||
| * | ||
| * Two lists of the same kind that follow each other merge into one list on | ||
| * re-parse unless the second uses a different bullet or delimiter, so the | ||
| * marker alternates along a run of adjacent lists. | ||
| */ | ||
| export function listMarker( | ||
| node: ElementNode, | ||
| state: State, | ||
| parent: ElementNode | undefined, | ||
| markers: [string, string] | ||
| ) { | ||
| const siblings: readonly unknown[] = parent ?? (state.context.tree as MarkdownDocument | undefined)?.nodes ?? [] | ||
| let byCollection = alternateLists.get(state.context) | ||
| if (!byCollection) alternateLists.set(state.context, (byCollection = new WeakMap())) | ||
| let alternates = byCollection.get(siblings) | ||
| if (!alternates) byCollection.set(siblings, (alternates = findAlternateLists(siblings, parent ? 2 : 0))) | ||
| return markers[alternates.has(sourceNode(node)) ? 1 : 0] | ||
| } | ||
|
|
||
| // One pass over the siblings: a list alternates when the one before it is a | ||
| // native list of the same kind that did not. | ||
| function findAlternateLists(siblings: readonly unknown[], start: number): Set<unknown> { | ||
| const alternates = new Set<unknown>() | ||
| let previousAlternates = false | ||
| for (let index = start; index < siblings.length; index++) { | ||
| const node = siblings[index] | ||
| const tag = Array.isArray(node) ? node[0] : undefined | ||
| const alternate: boolean = | ||
| (tag === 'ul' || tag === 'ol') && index > start && isNativeList(siblings[index - 1], tag) && !previousAlternates | ||
| if (alternate) alternates.add(node) | ||
| previousAlternates = alternate | ||
| } | ||
| return alternates | ||
| } | ||
|
|
||
| // A list with user attrs renders as a `::ul` / `::ol` component, which ends the run. | ||
| function isNativeList(node: unknown, tag: string): boolean { | ||
| return ( | ||
| Array.isArray(node) && node[0] === tag && !comarkAttributes(userBlockAttrs(tag, node[1] as Record<string, unknown>)) | ||
| ) | ||
| } |
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
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.
Escape text after hard-break children in setext headings.
For
Foo \n\\# bar\n===\n, the parser creates separate text,br, and text children. Thebrhandler emits a newline. The following# bartext receives no line-start escaping, and this statement escapes onlyFoo. The output therefore contains a new ATX heading instead of preserving one multiline heading. (raw.githubusercontent.com)Track line starts across heading children and escape text after
br. Keep the generated underline unescaped. Add this round-trip case.