Spaces:
Running
Running
thibaud frere commited on
Commit ·
b203e6a
1
Parent(s): b010293
remove data-language from code blocks
Browse filesFiles changed (2)
6 unmodified lines7891011121314151617181920212223242526272829303132333435363738394041424344454638 unmodified lines8586878889909192939495969798991006 unmodified linesif (!node || typeof node !== 'object') return;const children = Array.isArray(node.children) ? node.children : [];if (node.tagName === 'pre' && children.some(c => c.tagName === 'code')) {// Find code child and guess languageconst code = children.find(c => c.tagName === 'code');const collectClasses = (val) => Array.isArray(val) ? val.map(String) : (typeof val === 'string' ? String(val).split(/\s+/) : []);const fromClass = (names) => {const hit = names.find((n) => /^language-/.test(String(n)));return hit ? String(hit).replace(/^language-/, '') : '';};const codeClasses = collectClasses(code?.properties?.className);const preClasses = collectClasses(node?.properties?.className);const candidates = [code?.properties?.['data-language'],fromClass(codeClasses),node?.properties?.['data-language'],fromClass(preClasses),];let lang = candidates.find(Boolean) || '';const lower = String(lang).toLowerCase();const toExt = (s) => {switch (String(s).toLowerCase()) {case 'typescript': case 'ts': return 'ts';case 'tsx': return 'tsx';case 'javascript': case 'js': case 'node': return 'js';case 'jsx': return 'jsx';case 'python': case 'py': return 'py';case 'bash': case 'shell': case 'sh': return 'sh';case 'markdown': case 'md': return 'md';case 'yaml': case 'yml': return 'yml';case 'html': return 'html';case 'css': return 'css';case 'json': return 'json';default: return lower || '';}};const ext = toExt(lower);// Determine if single-line block: prefer Shiki lines, then text contentconst countLinesFromShiki = () => {const isLineEl = (el) => el && el.type === 'element' && el.tagName === 'span' && Array.isArray(el.properties?.className) && el.properties.className.includes('line');38 unmodified linesnode.__forceSingle = true;}}// Ensure CSS-only label works: set data-language on <code> and <pre>, and wrappercode.properties = code.properties || {};if (ext) code.properties['data-language'] = ext;node.properties = node.properties || {};if (ext) node.properties['data-language'] = ext;// Replace <pre> with wrapper div.code-card containing button + preconst wrapper = {type: 'element',tagName: 'div',properties: { className: ['code-card'].concat((isSingleLine || node.__forceSingle) ? ['no-copy'] : []), 'data-language': ext },children: (isSingleLine || node.__forceSingle) ? [ node ] : [{type: 'element',6 unmodified lines789101112131438 unmodified lines53545556575859606162636 unmodified linesif (!node || typeof node !== 'object') return;const children = Array.isArray(node.children) ? node.children : [];if (node.tagName === 'pre' && children.some(c => c.tagName === 'code')) {// Find code childconst code = children.find(c => c.tagName === 'code');// Determine if single-line block: prefer Shiki lines, then text contentconst countLinesFromShiki = () => {const isLineEl = (el) => el && el.type === 'element' && el.tagName === 'span' && Array.isArray(el.properties?.className) && el.properties.className.includes('line');38 unmodified linesnode.__forceSingle = true;}}// Replace <pre> with wrapper div.code-card containing button + preconst wrapper = {type: 'element',tagName: 'div',properties: { className: ['code-card'].concat((isSingleLine || node.__forceSingle) ? ['no-copy'] : []) },children: (isSingleLine || node.__forceSingle) ? [ node ] : [{type: 'element',
162 unmodified lines163164165166167168169170171172173174175176177178179180181182183184185186187188162 unmodified linespadding: 4px 6px;pointer-events: none;} *//* Fallback if Shiki uses data-lang instead of data-language */.astro-code[data-lang]::after { content: attr(data-lang); }/* Normalize to extensions for common languages */.astro-code[data-language="typescript"]::after { content: "ts"; }.astro-code[data-language="tsx"]::after { content: "tsx"; }.astro-code[data-language="javascript"]::after,.astro-code[data-language="node"]::after,.astro-code[data-language="jsx"]::after { content: "js"; }.astro-code[data-language="python"]::after { content: "py"; }.astro-code[data-language="bash"]::after,.astro-code[data-language="shell"]::after,.astro-code[data-language="sh"]::after { content: "sh"; }.astro-code[data-language="markdown"]::after { content: "md"; }.astro-code[data-language="yaml"]::after,.astro-code[data-language="yml"]::after { content: "yml"; }.astro-code[data-language="html"]::after { content: "html"; }.astro-code[data-language="css"]::after { content: "css"; }.astro-code[data-language="json"]::after { content: "json"; }/* In Accordions, keep same bottom-right placement */.accordion .astro-code::after { right: 0; bottom: 0; }162 unmodified lines163164165166167168162 unmodified linespadding: 4px 6px;pointer-events: none;} *//* In Accordions, keep same bottom-right placement */.accordion .astro-code::after { right: 0; bottom: 0; }
app/plugins/rehype/code-copy-and-label.mjs+2-39
app/src/styles/components/_code.css+1-21