Repository navigation
feat(demo): highlighted code blocks with labels, tabs and Copy - #31
Merged
Merged
Conversation
The demo pages' snippets used the page's pale surface colour in the prose's ink, and consecutive ones touched, so they were hard to tell from the text. A Vite plugin now highlights every <pre class="code" data-lang> with Shiki when a page is served or built: a dark panel in both themes, a label naming the language or file, tabs for alternatives (npm, pnpm, yarn), and a copy button. The theme names roles that leave the output as classes, since the site's style-src 'self' refuses the style attributes highlighters write; only a 1.6 kB script for the tabs and Copy reaches the browser. Closes #30. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
…hout stripping tags CodeQL flagged the tab group's pattern for backtracking (js/redos) and the test's tag-stripping replace as incomplete sanitization. A group now runs to the first </div>, which a block's escaped code cannot contain, and the test reads the text between tags instead of removing them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #30.
The demo pages' code snippets now stand apart from the text. Each block is a dark panel in both themes, highlighted, with a label, tabs where there are alternatives, and a Copy button.
How
Authoring: a page writes
<pre class="code" data-lang="sh|js|html|xml|text">.data-titlenames a block by a file instead of a language, such ascmi5.xmlon the cmi5 page.<div class="code-tabs">that each carrydata-tabbecome tabs. The setup page's install command is now npm / pnpm / yarn.Build:
apps/demo/scripts/lib/code-blocks.mjsis atransformIndexHtmlhook that runs after the site URL is filled in.<figure>with a head (label, tabs, Copy).style-src 'self', and Shiki's themes write astyleattribute on every token. So the theme is our own: its colours name roles (var(--tk-string)), and a transformer turns each into a class.player-page.css: tag, attribute, string, keyword, function, constant, comment, punctuation.Page:
apps/demo/demo/code-blocks.js(1.6 kB built) works the tabs and Copy.localStorage.Style:
#141a23).Pages: setup, embed, examples, normalize and cmi5. Every block is labelled, and the five pages load the script.
Testing
tests/code-blocks.test.ts, Chromiumpnpm build:demo, thenpreview:demowith the production headers, five pagessetup.htmlstyleattributeThe unit tests check every snippet on the five pages: after highlighting, each says exactly what the page wrote, which is what Copy hands over. They also check that no
styleattribute appears, the tab markup (ids,aria-controls, the first tab selected), file titles, and unknown languages. The browser test covers clicks, the arrow keys, End, the choice surviving a reload, and Copy.I compared screenshots before and after on the setup page, in both themes, at desktop and phone widths, and checked every block on the other four pages.
Release
None: demo site only. Vercel deploys it on merge.
🤖 Generated with Claude Code