Spaces:
Running
Running
thibaud frere
commited on
Commit
·
b203e6a
1
Parent(s):
b010293
remove data-language from code blocks
Browse files
app/plugins/rehype/code-copy-and-label.mjs
CHANGED
|
@@ -7,40 +7,8 @@ export default function rehypeCodeCopyAndLabel() {
|
|
| 7 |
if (!node || typeof node !== 'object') return;
|
| 8 |
const children = Array.isArray(node.children) ? node.children : [];
|
| 9 |
if (node.tagName === 'pre' && children.some(c => c.tagName === 'code')) {
|
| 10 |
-
// Find code child
|
| 11 |
const code = children.find(c => c.tagName === 'code');
|
| 12 |
-
const collectClasses = (val) => Array.isArray(val) ? val.map(String) : (typeof val === 'string' ? String(val).split(/\s+/) : []);
|
| 13 |
-
const fromClass = (names) => {
|
| 14 |
-
const hit = names.find((n) => /^language-/.test(String(n)));
|
| 15 |
-
return hit ? String(hit).replace(/^language-/, '') : '';
|
| 16 |
-
};
|
| 17 |
-
const codeClasses = collectClasses(code?.properties?.className);
|
| 18 |
-
const preClasses = collectClasses(node?.properties?.className);
|
| 19 |
-
const candidates = [
|
| 20 |
-
code?.properties?.['data-language'],
|
| 21 |
-
fromClass(codeClasses),
|
| 22 |
-
node?.properties?.['data-language'],
|
| 23 |
-
fromClass(preClasses),
|
| 24 |
-
];
|
| 25 |
-
let lang = candidates.find(Boolean) || '';
|
| 26 |
-
const lower = String(lang).toLowerCase();
|
| 27 |
-
const toExt = (s) => {
|
| 28 |
-
switch (String(s).toLowerCase()) {
|
| 29 |
-
case 'typescript': case 'ts': return 'ts';
|
| 30 |
-
case 'tsx': return 'tsx';
|
| 31 |
-
case 'javascript': case 'js': case 'node': return 'js';
|
| 32 |
-
case 'jsx': return 'jsx';
|
| 33 |
-
case 'python': case 'py': return 'py';
|
| 34 |
-
case 'bash': case 'shell': case 'sh': return 'sh';
|
| 35 |
-
case 'markdown': case 'md': return 'md';
|
| 36 |
-
case 'yaml': case 'yml': return 'yml';
|
| 37 |
-
case 'html': return 'html';
|
| 38 |
-
case 'css': return 'css';
|
| 39 |
-
case 'json': return 'json';
|
| 40 |
-
default: return lower || '';
|
| 41 |
-
}
|
| 42 |
-
};
|
| 43 |
-
const ext = toExt(lower);
|
| 44 |
// Determine if single-line block: prefer Shiki lines, then text content
|
| 45 |
const countLinesFromShiki = () => {
|
| 46 |
const isLineEl = (el) => el && el.type === 'element' && el.tagName === 'span' && Array.isArray(el.properties?.className) && el.properties.className.includes('line');
|
|
@@ -85,16 +53,11 @@ export default function rehypeCodeCopyAndLabel() {
|
|
| 85 |
node.__forceSingle = true;
|
| 86 |
}
|
| 87 |
}
|
| 88 |
-
// Ensure CSS-only label works: set data-language on <code> and <pre>, and wrapper
|
| 89 |
-
code.properties = code.properties || {};
|
| 90 |
-
if (ext) code.properties['data-language'] = ext;
|
| 91 |
-
node.properties = node.properties || {};
|
| 92 |
-
if (ext) node.properties['data-language'] = ext;
|
| 93 |
// Replace <pre> with wrapper div.code-card containing button + pre
|
| 94 |
const wrapper = {
|
| 95 |
type: 'element',
|
| 96 |
tagName: 'div',
|
| 97 |
-
properties: { className: ['code-card'].concat((isSingleLine || node.__forceSingle) ? ['no-copy'] : [])
|
| 98 |
children: (isSingleLine || node.__forceSingle) ? [ node ] : [
|
| 99 |
{
|
| 100 |
type: 'element',
|
|
|
|
| 7 |
if (!node || typeof node !== 'object') return;
|
| 8 |
const children = Array.isArray(node.children) ? node.children : [];
|
| 9 |
if (node.tagName === 'pre' && children.some(c => c.tagName === 'code')) {
|
| 10 |
+
// Find code child
|
| 11 |
const code = children.find(c => c.tagName === 'code');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
// Determine if single-line block: prefer Shiki lines, then text content
|
| 13 |
const countLinesFromShiki = () => {
|
| 14 |
const isLineEl = (el) => el && el.type === 'element' && el.tagName === 'span' && Array.isArray(el.properties?.className) && el.properties.className.includes('line');
|
|
|
|
| 53 |
node.__forceSingle = true;
|
| 54 |
}
|
| 55 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
// Replace <pre> with wrapper div.code-card containing button + pre
|
| 57 |
const wrapper = {
|
| 58 |
type: 'element',
|
| 59 |
tagName: 'div',
|
| 60 |
+
properties: { className: ['code-card'].concat((isSingleLine || node.__forceSingle) ? ['no-copy'] : []) },
|
| 61 |
children: (isSingleLine || node.__forceSingle) ? [ node ] : [
|
| 62 |
{
|
| 63 |
type: 'element',
|
app/src/styles/components/_code.css
CHANGED
|
@@ -163,26 +163,6 @@ section.content-grid pre code {
|
|
| 163 |
padding: 4px 6px;
|
| 164 |
pointer-events: none;
|
| 165 |
} */
|
| 166 |
-
|
| 167 |
-
/* Fallback if Shiki uses data-lang instead of data-language */
|
| 168 |
-
.astro-code[data-lang]::after { content: attr(data-lang); }
|
| 169 |
-
|
| 170 |
-
/* Normalize to extensions for common languages */
|
| 171 |
-
.astro-code[data-language="typescript"]::after { content: "ts"; }
|
| 172 |
-
.astro-code[data-language="tsx"]::after { content: "tsx"; }
|
| 173 |
-
.astro-code[data-language="javascript"]::after,
|
| 174 |
-
.astro-code[data-language="node"]::after,
|
| 175 |
-
.astro-code[data-language="jsx"]::after { content: "js"; }
|
| 176 |
-
.astro-code[data-language="python"]::after { content: "py"; }
|
| 177 |
-
.astro-code[data-language="bash"]::after,
|
| 178 |
-
.astro-code[data-language="shell"]::after,
|
| 179 |
-
.astro-code[data-language="sh"]::after { content: "sh"; }
|
| 180 |
-
.astro-code[data-language="markdown"]::after { content: "md"; }
|
| 181 |
-
.astro-code[data-language="yaml"]::after,
|
| 182 |
-
.astro-code[data-language="yml"]::after { content: "yml"; }
|
| 183 |
-
.astro-code[data-language="html"]::after { content: "html"; }
|
| 184 |
-
.astro-code[data-language="css"]::after { content: "css"; }
|
| 185 |
-
.astro-code[data-language="json"]::after { content: "json"; }
|
| 186 |
-
|
| 187 |
/* In Accordions, keep same bottom-right placement */
|
| 188 |
.accordion .astro-code::after { right: 0; bottom: 0; }
|
|
|
|
| 163 |
padding: 4px 6px;
|
| 164 |
pointer-events: none;
|
| 165 |
} */
|
| 166 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
/* In Accordions, keep same bottom-right placement */
|
| 168 |
.accordion .astro-code::after { right: 0; bottom: 0; }
|