1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-22 07:49:38 +01:00
deka-dom-el/docs_src/components/code.html.js

64 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-11-10 17:15:59 +01:00
import { registerClientFile, styles } from "../ssr.js";
2023-11-23 16:30:20 +01:00
const host= "."+code.name;
styles.css`
${host}{
2023-11-10 17:15:59 +01:00
--shiki-color-text: #e9eded;
--shiki-color-background: #212121;
--shiki-token-constant: #82b1ff;
--shiki-token-string: #c3e88d;
--shiki-token-comment: #546e7a;
--shiki-token-keyword: #c792ea;
--shiki-token-parameter: #AA0000;
--shiki-token-function: #80cbae;
--shiki-token-string-expression: #c3e88d;
--shiki-token-punctuation: var(--code);
--shiki-token-link: #EE0000;
white-space: pre;
2023-11-24 17:02:16 +01:00
overflow: scroll;
2023-11-10 17:15:59 +01:00
}
2023-11-23 16:30:20 +01:00
${host}[data-js=todo]{
2023-11-10 17:15:59 +01:00
border: 1px solid var(--border);
border-radius: var(--standard-border-radius);
margin-bottom: 1rem;
${/* to fix shift when → dataJS=done */""}
margin-top: 18.4px;
padding: 1rem 1.4rem;
}
`;
import { el } from "deka-dom-el";
/**
* Prints code to the page and registers flems to make it interactive.
* @param {object} attrs
* @param {string} [attrs.id]
* @param {string} [attrs.className]
2023-11-24 17:02:16 +01:00
* @param {URL} [attrs.src] Example code file path
* @param {string} [attrs.content] Example code
2023-11-10 17:15:59 +01:00
* @param {"js"|"ts"|"html"|"css"} [attrs.language="js"] Language of the code
* @param {string} [attrs.page_id] ID of the page, if setted it registers shiki
* */
2023-11-24 17:02:16 +01:00
export function code({ id, src, content, language= "js", className= host.slice(1), page_id }){
if(src) content= s.cat(src);
2023-11-10 17:15:59 +01:00
let dataJS;
if(page_id){
registerClientPart(page_id);
dataJS= "todo";
}
return el("div", { id, className, dataJS }).append(
el("code", { className: "language-"+language, textContent: content })
);
}
let is_registered= {};
/** @param {string} page_id */
function registerClientPart(page_id){
if(is_registered[page_id]) return;
document.head.append(
el("script", { src: "https://cdn.jsdelivr.net/npm/shiki", defer: true }),
);
registerClientFile(
new URL("./code.js.js", import.meta.url),
el("script", { type: "module" })
);
is_registered[page_id]= true;
}