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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,5 +42,11 @@ export async function heading(node: ElementNode, state: State) { | |
| const attrs = comarkAttributes(id === undefined || id === autoId ? rest : { id, ...rest }) | ||
| const suffix = attrs ? ` ${attrs}` : '' | ||
|
|
||
| // An ATX heading ends at the line break, so a multi-line h1/h2 (setext | ||
| // source) keeps its underline form. | ||
| if (level <= 2 && content.includes('\n')) { | ||
| return content + suffix + '\n' + (level === 1 ? '===' : '---') + state.context.blockSeparator | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Escape the first content line before emitting setext syntax. For This branch emits
|
||
| } | ||
|
|
||
| return '#'.repeat(level) + ' ' + content + suffix + state.context.blockSeparator | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,15 +2,17 @@ import type { State } from 'comark/render' | |
| import type { ElementNode, Node } from 'comark' | ||
| import { indent } from '../../../utils/index.ts' | ||
| import { comarkAttributes, userBlockAttrs } from '../attributes.ts' | ||
| import { listMarker } from '../list.ts' | ||
|
|
||
| export async function ol(node: ElementNode, state: State) { | ||
| export async function ol(node: ElementNode, state: State, parent?: ElementNode) { | ||
| const children = node.slice(2) as Node[] | ||
|
|
||
| // `start` is carried by the native numbering; IMPLICIT_ATTRS drops it from user attrs. | ||
| const start = Number((node[1] as Record<string, unknown>).start) | ||
| const order = Number.isInteger(start) && start >= 1 ? start : 1 | ||
| const order = Number.isInteger(start) && start >= 0 ? start : 1 | ||
| const marker = listMarker(node, state, parent, ['.', ')']) | ||
|
|
||
| const revert = state.applyContext({ list: true, order, listIndent: 3 }) | ||
| const revert = state.applyContext({ list: true, order, listMarker: marker }) | ||
|
|
||
| let result = '' | ||
| for (const child of children) { | ||
|
|
@@ -25,13 +27,13 @@ export async function ol(node: ElementNode, state: State) { | |
| const attrs = comarkAttributes(userBlockAttrs('ol', node[1] as Record<string, unknown>)) | ||
| if (attrs) { | ||
| if (revert.list) { | ||
| return '\n' + indent(`::ol${attrs}\n${result}\n::`, { width: (revert.listIndent as number) || 2 }) | ||
| return '\n' + indent(`::ol${attrs}\n${result}\n::`, { width: (state.context.listIndent as number) || 2 }) | ||
| } | ||
| return `::ol${attrs}\n${result}\n::` + state.context.blockSeparator | ||
| } | ||
|
|
||
| if (revert.list) { | ||
| result = '\n' + indent(result, { width: (revert.listIndent as number) || 2 }) | ||
| result = '\n' + indent(result, { width: (state.context.listIndent as number) || 2 }) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Separate a nested zero-start list from preceding paragraph text. For an item containing CommonMark does not allow an ordered list starting at
|
||
| } else { | ||
| result = result + state.context.blockSeparator | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import type { State } from 'comark/render' | ||
| import type { ElementNode, MarkdownDocument, Node } from 'comark' | ||
| import { comarkAttributes, userBlockAttrs } from './attributes.ts' | ||
|
|
||
| /** | ||
| * 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 = parent | ||
| ? (parent.slice(2) as Node[]) | ||
| : ((state.context.tree as MarkdownDocument | undefined)?.nodes ?? []) | ||
| let alternate = false | ||
| for (let index = siblings.indexOf(node); index > 0 && isNativeList(siblings[index - 1], node[0]); index--) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compute sibling markers once per sibling collection. Each list repeats As per coding guidelines, “Linear time complexity” and “Minimize allocations” apply here.
|
||
| alternate = !alternate | ||
| } | ||
| return markers[alternate ? 1 : 0] | ||
| } | ||
|
|
||
| // A list with user attrs renders as a `::ul` / `::ol` component, which ends the run. | ||
| function isNativeList(node: Node | undefined, tag: string): boolean { | ||
| return ( | ||
| Array.isArray(node) && node[0] === tag && !comarkAttributes(userBlockAttrs(tag, node[1] as Record<string, unknown>)) | ||
| ) | ||
| } | ||
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.
Check delimiter flanking before selecting
_.For
_foo *bar*baz_, this emits*foo _bar_baz*. The inner closing underscore falls between letters, so the inner emphasis becomes literal text. CommonMark allows intraword*emphasis but not intraword_emphasis. Select delimiters using the surrounding content, and add this round-trip case. (spec.commonmark.org)