|
|
@@ -21,6 +21,24 @@ export interface MarkdownHeadingLine extends MarkdownProseLine {
|
|
|
text: string
|
|
|
}
|
|
|
|
|
|
+/** One code block from a parsed Markdown source. */
|
|
|
+export interface MarkdownFence {
|
|
|
+ /** 1-based source line of the opening fence. */
|
|
|
+ line: number
|
|
|
+ /** Info-string language (its first word), null on a bare or indented block. */
|
|
|
+ lang: string | null
|
|
|
+ /** Full info string (e.g. `ts ignore-check`), '' on a bare or indented block. */
|
|
|
+ info: string
|
|
|
+ /** Block body without the fence delimiters. */
|
|
|
+ code: string
|
|
|
+ /**
|
|
|
+ * Whether a closing fence delimiter terminates the block — mdast silently
|
|
|
+ * closes an unterminated fence at end of file. False on indented
|
|
|
+ * (non-fenced) blocks, whose end line is code.
|
|
|
+ */
|
|
|
+ closed: boolean
|
|
|
+}
|
|
|
+
|
|
|
/** Parse GitHub-flavored Markdown with the repository's standard extensions. */
|
|
|
export function parseMarkdown(source: string): Nodes {
|
|
|
return fromMarkdown(source, { extensions: [gfm()], mdastExtensions: [gfmFromMarkdown()] })
|
|
|
@@ -38,6 +56,26 @@ export function visitMarkdown(node: Nodes, visitor: (node: Nodes) => boolean | v
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Extract every parsed code block with its info string, in document order.
|
|
|
+ * @param source - Markdown source to scan.
|
|
|
+ * @returns each block's opening line, language, info string, and body.
|
|
|
+ */
|
|
|
+export function markdownFences(source: string): MarkdownFence[] {
|
|
|
+ const lines = source.split('\n')
|
|
|
+ const fences: MarkdownFence[] = []
|
|
|
+ visitMarkdown(parseMarkdown(source), (node) => {
|
|
|
+ if (node.type !== 'code' || node.position === undefined) return
|
|
|
+ const lang = node.lang ?? null
|
|
|
+ const meta = node.meta ?? ''
|
|
|
+ const info = lang === null ? '' : meta === '' ? lang : `${lang} ${meta}`
|
|
|
+ const endLine = lines[node.position.end.line - 1] ?? ''
|
|
|
+ const closed = /^ {0,3}(`{3,}|~{3,})\s*$/.test(endLine)
|
|
|
+ fences.push({ line: node.position.start.line, lang, info, code: node.value, closed })
|
|
|
+ })
|
|
|
+ return fences
|
|
|
+}
|
|
|
+
|
|
|
/** Text a reader sees from one Markdown node; raw HTML itself contributes none. */
|
|
|
function renderedText(node: Nodes): string {
|
|
|
if (node.type === 'text' || node.type === 'inlineCode') return node.value
|
|
|
@@ -115,27 +153,22 @@ function hasRenderedTextOutsideComments(raw: string, ranges: readonly ColumnRang
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Return source lines outside backtick or tilde fences and HTML comments.
|
|
|
+ * Return source lines outside code blocks and HTML comments.
|
|
|
* @param source - Markdown source whose prose should be retained verbatim.
|
|
|
* @returns unfenced lines with their original 1-based locations.
|
|
|
*/
|
|
|
export function markdownProseLines(source: string): MarkdownProseLine[] {
|
|
|
- let fence: { marker: '`' | '~'; length: number } | undefined
|
|
|
- const kept: MarkdownProseLine[] = []
|
|
|
const rawLines = source.split('\n')
|
|
|
const comments = htmlCommentRanges(source, rawLines)
|
|
|
+ const fenced = new Set<number>()
|
|
|
+ visitMarkdown(parseMarkdown(source), (node) => {
|
|
|
+ if (node.type !== 'code' || node.position === undefined) return
|
|
|
+ for (let line = node.position.start.line; line <= node.position.end.line; line += 1) fenced.add(line)
|
|
|
+ })
|
|
|
+ const kept: MarkdownProseLine[] = []
|
|
|
rawLines.forEach((raw, i) => {
|
|
|
- const token = /^ {0,3}(`{3,}|~{3,})/.exec(raw)?.[1]
|
|
|
- if (token !== undefined) {
|
|
|
- const marker = token[0] as '`' | '~'
|
|
|
- if (fence === undefined) {
|
|
|
- fence = { marker, length: token.length }
|
|
|
- } else if (marker === fence.marker && token.length >= fence.length) {
|
|
|
- fence = undefined
|
|
|
- }
|
|
|
- return
|
|
|
- }
|
|
|
- if (fence === undefined && hasRenderedTextOutsideComments(raw, comments.get(i + 1))) {
|
|
|
+ if (fenced.has(i + 1)) return
|
|
|
+ if (hasRenderedTextOutsideComments(raw, comments.get(i + 1))) {
|
|
|
kept.push({ index: i + 1, raw })
|
|
|
}
|
|
|
})
|