Repository navigation
fix(web): footnotes in quoted Markdown tables keep their links in long files #48484
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
6a031cd
bbf03ea
42cab3f
b38b5e8
61a793b
387f579
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,7 @@ const LIST_ITEM = /^( {0,3}(?:[-*+]|\d{1,9}[.)]))([ \t]+|(?=\r?$))/; | |||||
| // Further markers on an item's first line open nested items that start on the same line. | ||||||
| const NESTED_LIST_MARKER = /([-*+]|\d{1,9}[.)])([ \t]+|(?=\r?$))/gy; | ||||||
| const BLOCK_QUOTE = /^ {0,3}>/; | ||||||
| const QUOTE_MARKERS = /^(?: {0,3}>[ \t]?)*/; | ||||||
| const DISPLAY_MATH_OPEN = /^ {0,3}\$\$/; | ||||||
| // CommonMark HTML blocks of types 1-5 and display math run past blank lines until their closer. | ||||||
| const SPANNING_BLOCKS: ReadonlyArray<readonly [open: RegExp, close: RegExp]> = [ | ||||||
|
|
@@ -847,6 +848,73 @@ export function splitMarkdown(text: string, targetChars = CHUNK_TARGET_CHARS): S | |||||
| return splitMarkdownBlocks(text, targetChars); | ||||||
| } | ||||||
|
|
||||||
| interface QuoteLine { | ||||||
| readonly depth: number; | ||||||
| readonly column: number; | ||||||
| readonly text: string; | ||||||
| /** A delimiter row makes the previous line a table header. */ | ||||||
| readonly kind: "prose" | "literal" | "delimiter" | "row"; | ||||||
| /** An open fence, HTML block or indented code that the next line may continue. */ | ||||||
| readonly closer: ((line: string) => boolean) | "indented" | null; | ||||||
| readonly paragraph: boolean; | ||||||
| /** The open paragraph belongs to a list item, where a table cannot start lazily. */ | ||||||
| readonly inItem: boolean; | ||||||
| } | ||||||
|
|
||||||
| /** Follows the blocks inside a quote far enough to find its tables and literal lines. */ | ||||||
| function scanQuoteLine( | ||||||
| source: string, | ||||||
| column: number, | ||||||
| previous: QuoteLine | null, | ||||||
| ): QuoteLine | null { | ||||||
| const markers = QUOTE_MARKERS.exec(source)![0]; | ||||||
| const depth = markers.split(">").length - 1; | ||||||
| if (depth === 0) return null; | ||||||
| const text = source.slice(markers.length); | ||||||
| const prev = previous?.depth === depth && previous.column === column ? previous : null; | ||||||
|
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. Preserve an open fence before interpreting more quote markers. Consider this quoted block, followed by enough text to separate its definition into another chunk: > ```
> > example
> ```
> Visible[^1]The second Strip the existing fence's container prefix before interpreting additional quote markers. Preserve normal nested-quote detection outside literal blocks.
|
||||||
| const line = { depth, column, text, paragraph: false, inItem: false }; | ||||||
| const blank = BLANK_LINE.test(text); | ||||||
| if (typeof prev?.closer === "function") { | ||||||
| return { ...line, kind: "literal", closer: prev.closer(text) ? null : prev.closer }; | ||||||
| } | ||||||
| const inParagraph = prev?.paragraph === true; | ||||||
| const indent = indentationColumn(/^[ \t]*/.exec(text)![0]); | ||||||
|
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. 🟡 Medium A tab after the quote marker is counted from column zero, so
Suggested change
🤖 Copy this AI Prompt to have your agent fix this: |
||||||
| if ((!blank && indent >= 4 && !inParagraph) || (blank && prev?.closer === "indented")) { | ||||||
| return { ...line, kind: "literal", closer: "indented" }; | ||||||
| } | ||||||
| if (blank) return { ...line, kind: "prose", closer: null }; | ||||||
| const closer = blockCloser(text); | ||||||
| if (closer !== undefined) return { ...line, kind: "literal", closer }; | ||||||
|
Comment on lines
+886
to
+887
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. Track standalone-tag HTML before recognizing quoted tables. For a long document containing this block and a later definition, the new scanner collects reference > <span>
> | `l | [^1] | r` |
> | - | - | - |The standalone Recognize
|
||||||
| if (opensNonParagraph(text)) return { ...line, kind: "prose", closer: null }; | ||||||
| const item = LIST_ITEM.exec(text); | ||||||
| if ((prev?.kind === "delimiter" || prev?.kind === "row") && !item) { | ||||||
| return { ...line, kind: "row", closer: null }; | ||||||
| } | ||||||
| const marker = item?.[1]!.trimStart(); | ||||||
| const opensItem = | ||||||
| item !== null && | ||||||
| (!inParagraph || | ||||||
| (!BLANK_LINE.test(text.slice(item[0].length)) && | ||||||
| (!/^\d/.test(marker!) || /^1[.)]$/.test(marker!)))); | ||||||
| if ( | ||||||
| inParagraph && | ||||||
| !prev.inItem && | ||||||
| !opensItem && | ||||||
| (text.includes("|") || text.includes(":")) && | ||||||
| TABLE_DELIMITER.test(text) && | ||||||
| splitTableCells(prev.text).length === splitTableCells(text).length | ||||||
| ) { | ||||||
| return { ...line, kind: "delimiter", closer: null }; | ||||||
| } | ||||||
| return { | ||||||
| ...line, | ||||||
| kind: "prose", | ||||||
| closer: null, | ||||||
| paragraph: true, | ||||||
| inItem: opensItem || (inParagraph && prev.inItem), | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| function splitMarkdownBlocks( | ||||||
| text: string, | ||||||
| targetChars: number, | ||||||
|
|
@@ -886,6 +954,10 @@ function splitMarkdownBlocks( | |||||
| let previousIndent = 0; | ||||||
| // Prose after a quote line may be its lazy continuation, which an underline cannot end. | ||||||
| let afterQuote = false; | ||||||
| let previousQuote: QuoteLine | null = null; | ||||||
| // Quoted table rows by line, without their quote markers, and where each table starts. | ||||||
| const quotedRows = new Map<number, string>(); | ||||||
| const tableHeaders = new Set<number>(); | ||||||
| let definitionChars = 0; | ||||||
| let footnoteChars = 0; | ||||||
| let footnoteCapExceeded = false; | ||||||
|
|
@@ -978,6 +1050,7 @@ function splitMarkdownBlocks( | |||||
| let closedBlock = false; | ||||||
| let plainProse = false; | ||||||
| let setextHeading = false; | ||||||
| let lineQuote: QuoteLine | null = null; | ||||||
|
|
||||||
| if (closes !== null) { | ||||||
| if (closes(continued)) { | ||||||
|
|
@@ -1345,6 +1418,22 @@ function splitMarkdownBlocks( | |||||
| : 1; | ||||||
| if (startsTable && isBodyLine[lineIndex - 1] === 1) isBodyLine[lineIndex - 1] = 4; | ||||||
| if (inTable && isBodyLine[lineIndex - 1] === 4) isBodyLine[lineIndex] = 4; | ||||||
| // A table inside a quote splits its cells before code spans, like one outside. | ||||||
| lineQuote = scanQuoteLine( | ||||||
| item && opensItem ? (itemFenceIndent ? itemContent : "") : content, | ||||||
| item && opensItem ? itemFenceIndent : contentColumn, | ||||||
| previousQuote, | ||||||
| ); | ||||||
| if (lineQuote?.kind === "literal") isBodyLine[lineIndex] = 0; | ||||||
| if (lineQuote?.kind === "delimiter") { | ||||||
| isBodyLine[lineIndex - 1] = 4; | ||||||
| quotedRows.set(lineIndex - 1, previousQuote!.text); | ||||||
| tableHeaders.add(lineIndex - 1); | ||||||
| } | ||||||
| if (lineQuote?.kind === "delimiter" || lineQuote?.kind === "row") { | ||||||
| isBodyLine[lineIndex] = 4; | ||||||
| quotedRows.set(lineIndex, lineQuote.text); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -1363,6 +1452,7 @@ function splitMarkdownBlocks( | |||||
| openWrappers = 0; | ||||||
| } | ||||||
| afterBlankLine = blank; | ||||||
| previousQuote = lineQuote; | ||||||
| afterClosedBlock = closedBlock; | ||||||
| afterPlainProse = plainProse; | ||||||
| previousIndent = indent; | ||||||
|
|
@@ -1403,8 +1493,10 @@ function splitMarkdownBlocks( | |||||
| for (let i = start; i < end; i++) { | ||||||
| if (isBodyLine[i] === 4) { | ||||||
| flushBlock(); | ||||||
| const cells = splitTableCells(sourceLines[i]!); | ||||||
| if (i === start || isBodyLine[i - 1] !== 4) tableColumns = cells.length; | ||||||
| const cells = splitTableCells(quotedRows.get(i) ?? sourceLines[i]!); | ||||||
| if (i === start || isBodyLine[i - 1] !== 4 || tableHeaders.has(i)) { | ||||||
| tableColumns = cells.length; | ||||||
| } | ||||||
| for (const cell of cells.slice(0, tableColumns)) | ||||||
| refs.push(...extractFootnoteRefs(cell, capturedFootnoteKeys, true)); | ||||||
| continue; | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { expect } from "e2e"; | ||
|
|
||
| import { test, WEB } from "./t3.ts"; | ||
|
|
||
| test( | ||
| "A quoted Markdown table links to a footnote elsewhere in the file", | ||
| WEB, | ||
| async ({ t3, app, browser, screen }) => { | ||
| const markdown = [ | ||
| "> | `left | Quoted reference[^note] | right` |", | ||
| "> | --- | --- | --- |", | ||
| "", | ||
| "Background paragraph. ".repeat(900), | ||
| "", | ||
| "## Notes", | ||
| "", | ||
| "[^note]: Shared explanation.", | ||
| "", | ||
| ].join("\n"); | ||
| const project = await t3.project("documentation", { "release.md": markdown }); | ||
| const thread = await t3.thread({ | ||
| project, | ||
| title: "Release notes", | ||
| messages: ['See :codex-file-citation{path="release.md"}'], | ||
| }); | ||
| await t3.open(thread); | ||
|
|
||
| await screen.getByRole("button", /^Agent See/).tap(); | ||
| await screen.getByRole("link", /^release\.md/).tap(); | ||
| await screen.getByRole("button", "Show rendered markdown").tap(); | ||
| await screen.getByText("Quoted reference", { exact: false }).waitFor(); | ||
| await app.screenshot("quoted-table-footnote"); | ||
| const reference = browser.locator("blockquote table a[data-footnote-ref]"); | ||
| await expect(reference).toHaveCount(1); | ||
| const noteInView = () => | ||
| browser.evaluate(() => { | ||
| const notes = document.querySelectorAll("section[data-footnotes] li"); | ||
| const note = [...notes].find((item) => item.textContent?.includes("Shared explanation.")); | ||
| const rect = note?.getBoundingClientRect(); | ||
| return rect !== undefined && rect.top >= 0 && rect.bottom <= window.innerHeight; | ||
| }); | ||
| await expect.poll(noteInView).toBe(false); | ||
| const clicked = await browser.evaluate(() => { | ||
| const anchor = document.querySelector<HTMLElement>("blockquote table a[data-footnote-ref]"); | ||
| anchor?.click(); | ||
| return anchor !== null; | ||
| }); | ||
| expect(clicked).toBe(true); | ||
| await expect.poll(noteInView).toBe(true); | ||
| }, | ||
| ); |
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.
🟡 Medium
src/markdownChunks.ts:874A quoted fence containing a line that starts with
>loses its open-fence state, so text after the closing fence is marked literal and references such as[^a]are omitted fromchunkRefs.scanQuoteLinecounts the content’s extra>as another quote level, causing the depth check to discard the previous fence state; while a literal block is open, strip only the quote markers belonging to the existing container.🤖 Copy this AI Prompt to have your agent fix this: