diff --git a/packages/comark/src/internal/stringify/handlers/emphesis.ts b/packages/comark/src/internal/stringify/handlers/emphesis.ts index 3a8d449b..7f988a18 100644 --- a/packages/comark/src/internal/stringify/handlers/emphesis.ts +++ b/packages/comark/src/internal/stringify/handlers/emphesis.ts @@ -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}` } diff --git a/packages/comark/src/internal/stringify/handlers/heading.ts b/packages/comark/src/internal/stringify/handlers/heading.ts index 84ff1ed9..01428337 100644 --- a/packages/comark/src/internal/stringify/handlers/heading.ts +++ b/packages/comark/src/internal/stringify/handlers/heading.ts @@ -3,6 +3,7 @@ import type { ElementNode, HeadingIdsOption } from 'comark' import { textContent } from '../../../utils/index.ts' import { createHeadingIdTracker, nextHeadingId, reserveHeadingId, type HeadingIdTracker } from '../../heading-id.ts' import { comarkAttributes } from '../attributes.ts' +import { escapeLeadingBlock } from '../state.ts' // Replays the parser's id generation across one render so each heading's // implicit id is known (`null` when ids are disabled). @@ -34,7 +35,7 @@ export async function heading(node: ElementNode, state: State) { const level = Number(tag.slice(1)) - const content = await state.flow(node, state) + let content = await state.flow(node, state) // The auto-generated id is implicit in `# Heading` markdown; only echo a custom one. const { id, ...rest } = node[1] as Record @@ -42,5 +43,16 @@ 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. Its text then starts the line, where a + // leading `#`, `>`, `-`... needs the escape the `# ` prefix made unnecessary. + if (level <= 2 && content.includes('\n')) { + if (typeof node[2] === 'string' && node[2]) { + const end = content.indexOf('\n') + content = escapeLeadingBlock(content.slice(0, end)) + content.slice(end) + } + return content + suffix + '\n' + (level === 1 ? '===' : '---') + state.context.blockSeparator + } + return '#'.repeat(level) + ' ' + content + suffix + state.context.blockSeparator } diff --git a/packages/comark/src/internal/stringify/handlers/li.ts b/packages/comark/src/internal/stringify/handlers/li.ts index 2ea5bf2f..11b9e913 100644 --- a/packages/comark/src/internal/stringify/handlers/li.ts +++ b/packages/comark/src/internal/stringify/handlers/li.ts @@ -4,14 +4,15 @@ import { indent } from '../../../utils/index.ts' import { comarkAttributes, userBlockAttrs } from '../attributes.ts' // Block elements that need explicit indentation in list items. -// Note: ol/ul are handled by their own handlers which manage indentation via listIndent context. +// Note: ol/ul are handled by their own handlers, which indent by the `listIndent` this item sets. const blockElements = new Set(['pre', 'blockquote', 'table']) export async function li(node: ElementNode, state: State) { const children = node.slice(2) as Node[] - const order = state.context.order - let prefix = order ? `${order}. ` : '- ' + const { order, listMarker } = state.context + const ordered = typeof order === 'number' + let prefix = ordered ? `${order}${listMarker || '.'} ` : `${listMarker || '-'} ` const className = (node[1].className as string) && Array.isArray(node[1].className) @@ -32,6 +33,10 @@ export async function li(node: ElementNode, state: State) { // Direct text children render sibling components inline. const hasInlineContent = children.some((child) => typeof child === 'string') + // A nested list must be indented to this item's content, whose column + // depends on the marker width (`1. ` vs `10. `). + const revert = state.applyContext({ listIndent: prefixWidth }) + let result = '' for (const child of children) { @@ -63,10 +68,12 @@ export async function li(node: ElementNode, state: State) { } result = result.trim() + state.applyContext(revert) + const attrs = comarkAttributes(userBlockAttrs('li', node[1] as Record)) const suffix = attrs ? ` ${attrs}` : '' - if (order) { + if (ordered) { state.applyContext({ order: order + 1 }) } diff --git a/packages/comark/src/internal/stringify/handlers/ol.ts b/packages/comark/src/internal/stringify/handlers/ol.ts index 760dd7e4..2ac3a3df 100644 --- a/packages/comark/src/internal/stringify/handlers/ol.ts +++ b/packages/comark/src/internal/stringify/handlers/ol.ts @@ -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).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,15 @@ export async function ol(node: ElementNode, state: State) { const attrs = comarkAttributes(userBlockAttrs('ol', node[1] as Record)) 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 }) + // Only a list that starts at 1 can interrupt the item's text: any other + // start needs a blank line before it. + result = (order === 1 ? '\n' : '\n\n') + indent(result, { width: (state.context.listIndent as number) || 2 }) } else { result = result + state.context.blockSeparator } diff --git a/packages/comark/src/internal/stringify/handlers/ul.ts b/packages/comark/src/internal/stringify/handlers/ul.ts index 42fbbc62..9abddc6d 100644 --- a/packages/comark/src/internal/stringify/handlers/ul.ts +++ b/packages/comark/src/internal/stringify/handlers/ul.ts @@ -2,11 +2,14 @@ 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 ul(node: ElementNode, state: State) { +export async function ul(node: ElementNode, state: State, parent?: ElementNode) { const children = node.slice(2) as Node[] - const revert = state.applyContext({ list: true, order: false, listIndent: 2 }) + const marker = listMarker(node, state, parent, ['-', '*']) + + const revert = state.applyContext({ list: true, order: false, listMarker: marker }) let result = '' for (const child of children) { @@ -21,13 +24,13 @@ export async function ul(node: ElementNode, state: State) { const attrs = comarkAttributes(userBlockAttrs('ul', node[1] as Record)) if (attrs) { if (revert.list) { - return '\n' + indent(`::ul${attrs}\n${result}\n::`, { width: (revert.listIndent as number) || 2 }) + return '\n' + indent(`::ul${attrs}\n${result}\n::`, { width: (state.context.listIndent as number) || 2 }) } return `::ul${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 }) } else { result = result + state.context.blockSeparator } diff --git a/packages/comark/src/internal/stringify/list.ts b/packages/comark/src/internal/stringify/list.ts new file mode 100644 index 00000000..5e1d8210 --- /dev/null +++ b/packages/comark/src/internal/stringify/list.ts @@ -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>>() + +/** + * 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 { + const alternates = new Set() + 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)) + ) +} diff --git a/packages/comark/src/internal/stringify/state.ts b/packages/comark/src/internal/stringify/state.ts index 0f03af60..1eeb3ffa 100644 --- a/packages/comark/src/internal/stringify/state.ts +++ b/packages/comark/src/internal/stringify/state.ts @@ -26,6 +26,17 @@ function findHandler(ctx: Context, node: ElementNode): NodeHandler | undefined { return userHandler } +// The tree node each `transformNode` copy was made from. +const sourceNodes = new WeakMap() + +/** + * The tree node `node` was rendered from: handlers get a copy when a + * `transformNode` hook changed it, and only the original is in its parent. + */ +export function sourceNode(node: ElementNode): ElementNode { + return sourceNodes.get(node) ?? node +} + /** * Render a single node. When `atLineStart` is true, leading block markers in * text nodes are escaped. @@ -86,7 +97,9 @@ export async function one(node: Node, state: State, parent?: ElementNode, atLine hasOwnAttrs = true props = transformed.props } - node = [transformed.tag, attrs, ...(node.slice(2) as Node[])] + const copy: ElementNode = [transformed.tag, attrs, ...(node.slice(2) as Node[])] + sourceNodes.set(copy, node) + node = copy } } if (hasOwnAttrs) state.renderData = { ...prevRenderData, props } @@ -382,7 +395,7 @@ function escapeInline(text: string): string { * heading. `*`, `_` and `~` based markers are already handled by * {@link escapeInline}; only the block-exclusive markers remain here. */ -function escapeLeadingBlock(line: string): string { +export function escapeLeadingBlock(line: string): string { // ATX heading: 1-6 `#` followed by a space/tab or the end of the line. if (/^#{1,6}([ \t]|$)/.test(line)) return `\\${line}` // Blockquote: `>` (a following space is optional). diff --git a/packages/comark/test/roundtrip-safety.test.ts b/packages/comark/test/roundtrip-safety.test.ts index 1920da38..4df84695 100644 --- a/packages/comark/test/roundtrip-safety.test.ts +++ b/packages/comark/test/roundtrip-safety.test.ts @@ -159,6 +159,37 @@ describe('component after a list', () => { }) }) +describe('lists, emphasis and headings', () => { + it.each([ + ['an ordered list that starts at 0', '0. zero\n1. one\n'], + ['adjacent bullet lists', '- a\n- b\n\n+ c\n\n* d\n'], + ['adjacent ordered lists', '1. a\n\n1) b\n'], + ['adjacent nested lists', '- a\n - b\n\n * c\n'], + ['a list nested under item 10', '10. ten\n - nested\n'], + [ + 'a list nested under the tenth item', + '1. a\n2. b\n3. c\n4. d\n5. e\n6. f\n7. g\n8. h\n9. i\n10. j\n - nested\n', + ], + ['a nested list that starts at 0 after text', '- parent\n\n 0. child\n'], + ['emphasis inside emphasis', '*_foo_*\n'], + ['emphasis that ends inside a word within emphasis', '_foo *bar*baz_\n'], + ['emphasis inside a link inside emphasis', '_x [a*b*](/u) y_\n'], + ['a multi-line heading', 'Foo\nbar\n===\n'], + ['a multi-line level 2 heading with attributes', 'Foo\nbar {.c}\n---\n'], + ['a multi-line heading that starts with an escaped marker', '\\# Foo\nbar\n===\n'], + ])('keeps %s', async (_, md) => { + const { t1, t2 } = await roundTrip(md) + expect(t2.nodes).toEqual(t1.nodes) + }) + + it('keeps adjacent lists apart when a transformNode hook copies them', async () => { + const t1 = await parseMarkdown('- a\n\n* b\n') + const rendered = await renderMarkdown(t1, { transformNode: ({ props }) => ({ props: { ...props } }) }) + const t2 = await parseMarkdown(rendered) + expect(t2.nodes).toEqual(t1.nodes) + }) +}) + describe('link and image serialization', () => { const doc = (node: unknown) => ({ frontmatter: {}, meta: {}, nodes: [['p', {}, node]] }) as unknown as MarkdownDocument diff --git a/test/bundle.test.ts b/test/bundle.test.ts index 557655dc..6cfe2a71 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -77,7 +77,7 @@ describe('package bundle size', { timeout: 60_000 }, () => { "@comark/react": "43.3k (87 files)", "@comark/svelte": "51.0k (95 files)", "@comark/vue": "62.7k (93 files)", - "comark": "451k (175 files)", + "comark": "455k (177 files)", } `) })