Repository navigation
fix(docs): preserve source text through syntax highlighting #971
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
Closed
rudycelekli
wants to merge
3
commits into
openclaw:main
from
rudycelekli:fix/peekaboo-docs-code-preservation-20261005
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
| import { spawnSync } from 'node:child_process'; | ||
| import test from 'node:test'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| const projectRoot = fileURLToPath(new URL('..', import.meta.url)); | ||
|
|
||
| function renderCode(t, language, source) { | ||
| const fixtureRoot = mkdtempSync(path.join(os.tmpdir(), 'peekaboo-docs-code-')); | ||
| t.after(() => rmSync(fixtureRoot, { recursive: true, force: true })); | ||
| mkdirSync(path.join(fixtureRoot, 'docs')); | ||
| writeFileSync(path.join(fixtureRoot, 'docs', 'index.md'), `# Fixture\n\n\`\`\`${language}\n${source}\n\`\`\`\n`); | ||
| const result = spawnSync(process.execPath, [path.join(projectRoot, 'scripts', 'build-docs-site.mjs')], { | ||
| cwd: fixtureRoot, encoding: 'utf8' | ||
| }); | ||
| assert.equal(result.status, 0, result.stderr || result.stdout); | ||
| const html = readFileSync(path.join(fixtureRoot, '_site', 'index.html'), 'utf8'); | ||
| const code = html.match(/<pre><code[^>]*>([\s\S]*?)<\/code><\/pre>/)?.[1]; | ||
| assert.ok(code !== undefined, 'missing rendered code block'); | ||
| // Remove only exact span wrappers that this renderer owns. Literal source | ||
| // markup remains escaped until these wrappers have been removed. | ||
| let text = code.replaceAll('</span>', ''); | ||
| for (const cls of ['hl-s', 'hl-c', 'hl-p', 'hl-f', 'hl-cmd', 'hl-n', 'hl-k', 'hl-m', 'hl-t']) { | ||
| text = text.replaceAll(`<span class="${cls}">`, ''); | ||
| } | ||
| const copyable = text.replace(/"/g, '"').replace(/'/g, "'") | ||
| .replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&'); | ||
| assert.equal(copyable, source); | ||
| return code; | ||
| } | ||
|
|
||
| test('syntax highlighting preserves private-use characters in valid shell paths and YAML keys', (t) => { | ||
| renderCode(t, 'bash', 'cat \ue000report\uf8ff.txt --lines 12'); | ||
| renderCode(t, 'yaml', '\ue000key\uf8ff: value'); | ||
| }); | ||
|
|
||
| test('syntax highlighting preserves blocks with more than 6400 highlighted tokens', (t) => { | ||
| const source = JSON.stringify(Array.from({ length: 7000 }, (_, index) => `value-${index}`)); | ||
| const html = renderCode(t, 'json', source); | ||
| assert.equal((html.match(/class="hl-s"/g) || []).length, 7000); | ||
| }); | ||
|
|
||
| test('source text and existing highlighting survive ordered pattern passes', (t) => { | ||
| const shell = renderCode(t, 'bash', 'peekaboo --app "Finder" 42 # npm <example>'); | ||
| assert.match(shell, /class="hl-cmd">peekaboo/); | ||
| assert.match(shell, /class="hl-f">--app/); | ||
| assert.match(shell, /class="hl-s">"Finder"/); | ||
| assert.match(shell, /class="hl-n">42/); | ||
| assert.match(shell, /class="hl-c"> # npm <example>/); | ||
| renderCode(t, 'bash', 'echo "<span class=\'hl-s\'>literal & text</span>"'); | ||
| renderCode(t, 'js', 'const value = "npm 12"; // git --flag'); | ||
| renderCode(t, 'swift', 'let value = "quoted <text>" // 42'); | ||
| renderCode(t, 'json', '{"name":"<example>","count":42,"enabled":true}'); | ||
| }); |
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.
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.
Anchors bind to fragments not lines
Low Severity
highlightFragmentsruns each pattern on leftover source slices, so^and$now mean fragment edges rather than the original line. Mid-line shell comments stop at an earlier quoted match, and later passes can restyle the remainder. Copyable text stays intact; only span assignment changes.Additional Locations (1)
scripts/build-docs-site.mjs#L776-L779Reviewed by Cursor Bugbot for commit f5e962d. Configure here.