import type { AdfNode } from '../adf/document.ts'
import type { LinkAttrs } from '../adf/mark-attributes.ts'
import { anchorOpener } from './anchor-element.ts'
import { languageClass } from './language-class.ts'
import { nodeAttrs, nodeContent } from '../adf/document.ts'
import { partsShownText, shownImage, shownLink, shownNote, shownText, shownUrl } from '../adf/shown-text.ts'
import { shownValue } from './escaping.ts'
// Compared by reference: each block makes one, so its content after a nested block starts a new shown block. `code`: this block, where it is a code block.
type BlockPart = { code: AdfNode | undefined }
type ShownBlock = { html: string; part: BlockPart }
// What a carry element shows of the node it holds, and all `htmlToAdf` accepts there, so a change here breaks reading stored HTML (docs/decisions.md §The formats are API).
export function carryContent(node: AdfNode, tag: 'div' | 'span'): string {
if (tag === 'span') return shownBlocks(node, '
').map(({ html }) => html).join('
')
return shownBlocks(node, '\n').map(({ html, part: { code } }) => (code === undefined ? `
${html}
` : `${html}`)).join('')
}
// What a carry element shows, block by block, as the writer panels picked it (docs/decisions.md §The HTML dialect).
function shownBlocks(root: AdfNode, codeLineFeed: string): ShownBlock[] {
const shown: ShownBlock[] = []
const pending: { node: AdfNode; part: BlockPart }[] = [{ node: root, part: { code: undefined } }]
for (let next = pending.pop(); next !== undefined; next = pending.pop()) {
const { node } = next
const part = partsShownText(node.type) ? { code: node.type === 'codeBlock' ? node : undefined } : next.part
const piece = shownPiece(node, part.code === undefined ? '