-
Notifications
You must be signed in to change notification settings - Fork 19
chore(docs): enhance code block handling and JSON-LD encoding #311
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| .DS_store | ||
| .vitest | ||
| .vitest-attachments | ||
| .angular | ||
| node_modules | ||
| dist | ||
| .lighthouse | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,75 @@ | ||
| import markdownIt from 'markdown-it'; | ||
| import markdownItLink from 'markdown-it-link-attributes'; | ||
|
|
||
| const LANGUAGE_NAMES = { | ||
| bash: 'Shell', | ||
| css: 'CSS', | ||
| go: 'Go', | ||
| html: 'HTML', | ||
| javascript: 'JavaScript', | ||
| js: 'JavaScript', | ||
| json: 'JSON', | ||
| markdown: 'Markdown', | ||
| md: 'Markdown', | ||
| python: 'Python', | ||
| shell: 'Shell', | ||
| sh: 'Shell', | ||
| toml: 'TOML', | ||
| ts: 'TypeScript', | ||
| tsx: 'TypeScript', | ||
| typescript: 'TypeScript', | ||
| xml: 'XML', | ||
| yaml: 'YAML', | ||
| yml: 'YAML', | ||
| zsh: 'Shell' | ||
| }; | ||
|
|
||
| const markdown = markdownIt({ | ||
| html: true, | ||
| breaks: false, | ||
| linkify: true, | ||
| highlight: function (str, lang) { | ||
| lang = lang === 'javascript' ? 'typescript' : lang; // alias javascript to typescript | ||
| const structuredData = getCodeStructuredData(str, lang); | ||
| const codeblockLanguage = markdown.utils.escapeHtml(lang === 'javascript' ? 'typescript' : lang); // alias javascript to typescript | ||
| return /* html */ ` | ||
| <div class="markdown-codeblock"> | ||
| <pre class="visually-hidden" aria-hidden="true"><code>${markdown.utils.escapeHtml(str)}</code></pre> | ||
| <nve-codeblock language="${lang}"><template>${markdown.utils.escapeHtml(str)}</template></nve-codeblock> | ||
| <script type="application/ld+json">${jsonLdEncode(structuredData)}</script> | ||
| <nve-codeblock language="${codeblockLanguage}"><pre aria-hidden="true"><code>${markdown.utils.escapeHtml(str).trim()}</code></pre></nve-codeblock> | ||
| <nve-copy-button class="markdown-copy-button" role="button" aria-label="copy" behavior-copy container="flat"></nve-copy-button> | ||
| </div> | ||
| <script type="module"> | ||
| document.querySelectorAll('.markdown-copy-button').forEach(button => { | ||
| const codeblock = button.previousElementSibling; | ||
| button.value = codeblock.querySelector('template').content.textContent.trim(); | ||
| button.value = codeblock.querySelector('pre code').textContent.trim(); | ||
| }); | ||
| </script>`; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Comment on lines
31
to
46
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. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- markdown renderer ---'
cat -n projects/site/src/_11ty/libraries/markdown.js | sed -n '1,115p'
printf '%s\n' '--- renderer integration ---'
rg -n -C 4 'markdown|markdown-it|markdownRenderer|render' projects/site/eleventy.config.js projects/site/src/_11ty --glob '*.js' --glob '*.ts' | head -260
printf '%s\n' '--- fence metadata with potentially breaking characters ---'
rg -n '^[[:space:]]*```[^[:space:]]*.*["<>=]' projects/site/src --glob '*.md' --glob '*.mdx' || trueRepository: NVIDIA/elements Length of output: 26559 Escape 🤖 Prompt for AI Agents |
||
| }); | ||
|
|
||
| function getCodeStructuredData(code, language) { | ||
| const languageName = language ? (LANGUAGE_NAMES[language.toLowerCase()] ?? language) : null; | ||
|
|
||
| return { | ||
| '@context': 'https://schema.org', | ||
| '@type': 'SoftwareSourceCode', | ||
| ...(languageName | ||
| ? { | ||
| programmingLanguage: { | ||
| '@type': 'ComputerLanguage', | ||
| name: languageName | ||
| } | ||
| } | ||
| : {}), | ||
| codeSampleType: 'code snippet', | ||
| encodingFormat: 'text/plain', | ||
| text: code | ||
| }; | ||
| } | ||
|
|
||
| function jsonLdEncode(value) { | ||
| return JSON.stringify(value).replaceAll('<', '\\u003c'); | ||
| } | ||
|
|
||
| markdown.renderer.rules.fence = function (tokens, idx, options, env, slf) { | ||
| const token = tokens[idx]; | ||
| const info = token.info ? markdown.utils.unescapeAll(token.info).trim() : ''; | ||
|
|
||
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.
This allows a pre/code element to be slotted instead of a template tag. This helps when the code content is automatically wrapped by 11ty and reduced duplicative code blocks in the output source.