-
Notifications
You must be signed in to change notification settings - Fork 1.1k
docs: fix raw markdown served to agents #6914
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4902540
docs: drop the redundant homepage prerenderRoutes call
benjamincanac 9ed44bd
docs: bump nuxt-agent-discovery to 0.3.0 and drop the remaining prereβ¦
benjamincanac 3fded8e
docs: fix tables, code previews, cards and inline components in the rβ¦
benjamincanac ac10cb8
docs: list the typography pages and the components index in llms.txt
benjamincanac 8ffbce3
docs: fix the raw markdown review findings
benjamincanac c256b91
docs: escape backslashes in table text and only pipes in code spans
benjamincanac 9ea37ce
docs: bump nuxt-agent-discovery to 0.4.0
benjamincanac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { textContent } from 'minimark' | ||
|
|
||
| /** | ||
| * Markdown builders for what `minimark/stringify` cannot express in its | ||
| * `markdown/html` format: it has no pipe-table handler, so a `table` node | ||
| * comes out as HTML, and its `pre` handler always opens a three-backtick | ||
| * fence, which code carrying fences of its own breaks out of. Both return a | ||
| * string to drop into the tree as a paragraph, which the stringifier passes | ||
| * through untouched. Links stay as written: `nuxt-agent-discovery` absolutizes | ||
| * the stringified document afterwards, table rows included. | ||
| */ | ||
|
|
||
| /** | ||
| * A backtick run that cannot be closed early by the text: one longer than | ||
| * any run of `minimum` or more backticks inside it, and at least `minimum`. | ||
| */ | ||
| function backticks(text: string, minimum: number): string { | ||
| const runs = Array.from(text.matchAll(new RegExp(`\`{${minimum},}`, 'g')), match => match[0].length) | ||
| return '`'.repeat(Math.max(minimum - 1, ...runs) + 1) | ||
| } | ||
|
|
||
| // GFM reads `\|` as a pipe anywhere in a cell, code spans included, while | ||
| // every other backslash inside a code span stays literal. So plain text gets | ||
| // both characters escaped and a code span only its pipes, joined rather than | ||
| // replaced so a literal backslash is never doubled there. | ||
| function escapeText(text: string): string { | ||
| return text.replace(/[\\|]/g, '\\$&') | ||
| } | ||
|
|
||
| function escapePipes(text: string): string { | ||
| return text.split('|').join('\\|') | ||
| } | ||
|
|
||
| /** Renders the inline nodes a table cell can hold to markdown. */ | ||
| function inlineMarkdown(node: any): string { | ||
| if (typeof node === 'string') return escapeText(node) | ||
| const [tag, attrs = {}, ...children] = node | ||
| const inner = () => children.map(inlineMarkdown).join('') | ||
|
|
||
| switch (tag) { | ||
| case 'code': { | ||
| const text = escapePipes(textContent(node)) | ||
| const fence = backticks(text, 1) | ||
| // A space keeps a leading or trailing backtick apart from the fence. | ||
| return /^`|`$/.test(text) ? `${fence} ${text} ${fence}` : `${fence}${text}${fence}` | ||
| } | ||
| case 'a': return `[${inner()}](${escapePipes(String(attrs.href ?? ''))})` | ||
| case 'strong': | ||
| case 'b': return `**${inner()}**` | ||
| case 'em': | ||
| case 'i': return `*${inner()}*` | ||
| case 'del': return `~~${inner()}~~` | ||
| case 'img': return `)})` | ||
| // The one line break a pipe cell can hold. | ||
| case 'br': return '<br>' | ||
| default: return inner() | ||
| } | ||
| } | ||
|
|
||
| function cell(node: any): string { | ||
| return inlineMarkdown(node).replace(/\s+/g, ' ').trim() | ||
| } | ||
|
|
||
| /** A `table` node as a GFM pipe table, the first row serving as header. */ | ||
| export function pipeTable(table: any[]): string { | ||
| const rows: string[][] = [] | ||
| for (const section of table.slice(2)) { | ||
| if (!Array.isArray(section)) continue | ||
| // Rows sit under `thead` and `tbody`; a bare `tr` is tolerated too. | ||
| for (const row of section[0] === 'tr' ? [section] : section.slice(2)) { | ||
| if (Array.isArray(row) && row[0] === 'tr') { | ||
| rows.push(row.slice(2).filter(child => Array.isArray(child)).map(cell)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const [header, ...body] = rows | ||
| if (!header?.length) return '' | ||
|
|
||
| const width = header.length | ||
| const line = (cells: string[]) => `| ${Array.from({ length: width }, (_, i) => cells[i] ?? '').join(' | ')} |` | ||
|
|
||
| return [line(header), line(header.map(() => '---')), ...body.map(line)].join('\n') | ||
| } | ||
|
|
||
| /** A fenced code block whose fence is longer than any fence-like run inside. */ | ||
| export function fencedBlock(code: string, language = '', filename?: string, meta?: string): string { | ||
| const fence = backticks(code, 3) | ||
| return `${fence}${language}${filename ? ` [${filename}]` : ''}${meta || ''}\n${code.trim()}\n${fence}` | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.