const a: Array+ +after +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "pre", + { + "$": { + "html": 1, + "block": 1 + } + }, + "const a: Array= [] +
const a: Array<string> = [] ++
after
+``` + +## Markdown + +```md +const a: Array<string> = [] ++ +after +``` diff --git a/packages/comark/SPEC/HTML/pre-verbatim.md b/packages/comark/SPEC/HTML/pre-verbatim.md new file mode 100644 index 000000000..4829d1729 --- /dev/null +++ b/packages/comark/SPEC/HTML/pre-verbatim.md @@ -0,0 +1,71 @@ +## Input + +```md +
+ const x = 1 + **not** + b ++ +after +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "pre", + { + "$": { + "html": 1, + "block": 1 + } + }, + "\n const x = 1\n **not**\n ", + [ + "b", + { + "$": { + "html": 1, + "block": 0 + } + }, + "b" + ], + "\n" + ], + [ + "p", + {}, + "after" + ] + ] +} +``` + +## HTML + +```html +
+ const x = 1 + **not** + b ++
after
+``` + +## Markdown + +```md ++ const x = 1 + **not** + b ++ +after +``` diff --git a/packages/comark/SPEC/HTML/raw-script-inline.md b/packages/comark/SPEC/HTML/raw-script-inline.md new file mode 100644 index 000000000..88e4b951a --- /dev/null +++ b/packages/comark/SPEC/HTML/raw-script-inline.md @@ -0,0 +1,46 @@ +## Input + +```md +a after +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "p", + {}, + "a ", + [ + "script", + { + "$": { + "html": 1, + "block": 0 + } + }, + "**x**" + ], + " after" + ] + ] +} +``` + +## HTML + +```html +
+ a after +
+``` + +## Markdown + +```md +a after +``` diff --git a/packages/comark/SPEC/HTML/raw-tag-exact-name-prefix.md b/packages/comark/SPEC/HTML/raw-tag-exact-name-prefix.md new file mode 100644 index 000000000..068bbff48 --- /dev/null +++ b/packages/comark/SPEC/HTML/raw-tag-exact-name-prefix.md @@ -0,0 +1,50 @@ +## Input + +```md +a
+ a
+ a
Check
Check
+Check
+on a block frame. */ pendingInline: Node[] + afterText?: boolean + /** Stack index of the nearest `p` or scope boundary at or below this frame, or -1. */ + pScope: number } /** Shared mutable context for a whole document walk. */ @@ -46,6 +49,9 @@ interface ProcessState { startLine: number /** Unclosed HTML open tags, outer → inner. Lives across blocks like the heading id stack. */ htmlStack: HtmlOpenFrame[] + closers?: CloserIndex + /** Set once a paragraph closed a tag whose closer may still arrive in a later stream chunk. */ + provisional?: boolean } type Processor = (tokens: Token[], start: number, state: ProcessState) => ProcessorResult @@ -69,10 +75,23 @@ function isInlineish(node: Node): boolean { return tag === null || inlineTags.has(tag as string) } +function isInlineHtml(node: Node): boolean { + if (typeof node === 'string') return false + const tag = node[0] + if (tag === null || inlineTags.has(tag) || tag === 'img' || tag === 'br') return true + return tag.includes('-') && node[1].$?.block === 0 +} + function flushPendingInline(frame: HtmlOpenFrame) { if (frame.pendingInline.length === 0) return const pending = frame.pendingInline frame.pendingInline = [] + const only = pending.length === 1 ? pending[0] : undefined + // A lone author-written element is a block of its own, not a paragraph. + if (Array.isArray(only) && only[1].$?.html && !isInlineish(only)) { + frame.children.push(only) + return + } frame.children.push(['p', {}, ...pending] as ElementNode) } @@ -112,11 +131,11 @@ function deliverBlock(state: ProcessState, node: Node | undefined): Node | undef } /** Nest an inline/finished node into the open HTML frame during processInline. */ -function deliverInline(state: ProcessState, node: Node | undefined): Node | undefined { +function deliverInline(state: ProcessState, node: Node | undefined, inline?: boolean): Node | undefined { if (node === undefined) return undefined if (state.htmlStack.length === 0) return node const top = state.htmlStack[state.htmlStack.length - 1] - if (top.block && isInlineish(node)) { + if (top.block && (inline ?? isInlineish(node))) { top.pendingInline.push(node) } else { flushPendingInline(top) @@ -144,7 +163,14 @@ function tryCommentTag(content: string, state: ProcessState): ProcessorResult | // Open comment (no close in this chunk) if (content.startsWith('')) { const body = content.slice(4) - state.htmlStack.push({ tag: null, attrs: {}, children: body ? [body] : [], block: true, pendingInline: [] }) + state.htmlStack.push({ + tag: null, + attrs: {}, + children: body ? [body] : [], + block: true, + pendingInline: [], + pScope: state.htmlStack.at(-1)?.pScope ?? -1, + }) return { nextIndex: -1, node: undefined } } @@ -157,6 +183,89 @@ function tryCommentTag(content: string, state: ProcessState): ProcessorResult | return null } +function closeFramesTo(state: ProcessState, idx: number): Node | undefined { + let closed: Node | undefined + while (state.htmlStack.length > idx) { + const node = frameToNode(state.htmlStack.pop()!) + if (state.htmlStack.length > idx) deliverInline(state, node) + else closed = node + } + return closed +} + +// Elements that stop a `
` from being closed by a following `
` (HTML "button scope"). +const P_SCOPE_BOUNDARY = new Set(['table', 'td', 'th', 'caption', 'button', 'template', 'object', 'marquee', 'applet']) + +function closeImpliedParagraph(token: Token, state: ProcessState): Node | undefined { + if ((token.content.charCodeAt(1) | 0x20) !== 0x70 /* p */ || !/^
]/i.test(token.content)) return undefined + const parsed = parseHtmlInline(token.content) + if (parsed.kind !== 'open' || parsed.tag !== 'p' || parsed.selfClosing) return undefined + const idx = state.htmlStack.at(-1)?.pScope ?? -1 + return idx >= 0 && state.htmlStack[idx].tag === 'p' ? closeFramesTo(state, idx) : undefined +} + +const firstProse = new WeakMap` (`$.html`) is content, not a wrapper.
+ if (
+ significantCount === 1 &&
+ Array.isArray(significant) &&
+ significant[0] === 'p' &&
+ !(significant[1] as ElementNodeAttributes | undefined)?.$?.html
+ ) {
// Lift the paragraph's attrs onto the parent so trailing `{attr}` survives the unwrap.
// Parent attrs take precedence so explicit component props aren't overridden.
const paragraphAttrs = significant[1] as Record` is content, nested tags included.
+ const authoredRaw = state.context.rawText === true && $.html === 1
+ if (tag === 'pre' || tag === 'textarea' || tag === 'script' || tag === 'style' || authoredRaw) {
+ oneLiner = true
+ }
+ // Re-indenting a paragraph would put spaces into the `` written inside it.
+ if (
+ tag === 'p' &&
+ children.some((child) => typeof child !== 'string' && (child?.[0] === 'pre' || child?.[0] === 'textarea'))
+ ) {
oneLiner = true
}
@@ -63,16 +72,28 @@ export async function html(node: ElementNode, state: State, parent?: ElementNode
const isSelfClose = selfCloseTags.has(String(tag))
- // Do not modify context if we are already in html mode
- const revert = state.applyContext({ inline: oneLiner })
+ const rawText = state.context.rawText === true || tag === 'pre' || tag === 'textarea'
+ const literal = $.html === 1 && ($.block === 1 || state.context.literal === true)
+ // Only changed keys go into the edit: every element renders through here.
+ const edit: Record