🆙 Add cms i using 🆙

This commit is contained in:
Remco
2025-11-25 22:42:56 +01:00
parent 94704e0925
commit d44196149e
35591 changed files with 3601123 additions and 0 deletions
@@ -0,0 +1,166 @@
const FontStyle = {
NotSet: -1,
None: 0,
Italic: 1,
Bold: 2,
Underline: 4,
};
const FONT_STYLE_TO_CSS = {
[FontStyle.Italic]: "font-style: italic",
[FontStyle.Bold]: "font-weight: bold",
[FontStyle.Underline]: "text-decoration: underline",
};
const renderToHtml = function (lines, options = {}) {
const theme = options.theme;
const themes = options.themes;
const highlightedLines = makeHighlightSet(options.highlightLines);
const addLines = makeHighlightSet(options.addLines);
const deleteLines = makeHighlightSet(options.deleteLines);
const focusLines = makeHighlightSet(options.focusLines);
let className = "shiki";
if (highlightedLines.size) {
className += " highlighted";
}
if (addLines.size) {
className += " added";
}
if (deleteLines.size) {
className += " deleted";
}
if (focusLines.size) {
className += " focus";
}
let html = "";
if (theme) {
html += `<pre class="${className}" style="background-color: ${theme.theme.bg}">`;
} else if (themes) {
const backgroundStyles = Object.entries(themes).map(
([theme, theme$]) => {
if (theme === "light") {
return `background-color:${theme$.theme.bg};`;
}
return `--shiki-${theme}-bg:${theme$.theme.bg};`;
}
);
const foregroundStyles = Object.entries(themes).map(
([theme, theme$]) => {
if (theme === "light") {
return `color:${theme$.theme.fg};`;
}
return `--shiki-${theme}:${theme$.theme.fg};`;
}
);
const classes = `${className} shiki-themes ${Object.values(themes)
.map((theme) => theme.theme.name)
.join(" ")}`;
html += `<pre class="${classes}" style="${backgroundStyles.join(
""
)}${foregroundStyles.join("")}">`;
}
if (options.langId) {
html += `<div class="language-id">${options.langId}</div>`;
}
html += `<code>`;
lines.forEach((l, index) => {
const lineNumber = index + 1;
let lineClass = "line";
if (highlightedLines.has(lineNumber)) {
lineClass += " highlight";
}
if (addLines.has(lineNumber)) {
lineClass += " add";
}
if (deleteLines.has(lineNumber)) {
lineClass += " del";
}
if (focusLines.has(lineNumber)) {
lineClass += " focus";
}
html += `<span class="${lineClass.trim()}">`;
l.forEach((token) => {
const cssDeclarations = [];
if (theme) {
cssDeclarations.push(`color:${token.color || theme.theme.fg}`);
} else if (themes) {
/**
* The `htmlStyle` property can be a `string` or an `object`. The `string` representation is deprecated.
* @see https://github.com/search?q=repo%3Ashikijs%2Fshiki+htmlStyle&type=code
*/
if (typeof token.htmlStyle === "string") {
cssDeclarations.push(token.htmlStyle);
} else if (typeof token.htmlStyle === "object") {
for (const [key, value] of Object.entries(
token.htmlStyle
)) {
cssDeclarations.push(`${key}:${value}`);
}
}
}
if (token.fontStyle > FontStyle.None) {
cssDeclarations.push(FONT_STYLE_TO_CSS[token.fontStyle]);
}
html += `<span style="${cssDeclarations.join(";")}">${escapeHtml(
token.content
)}</span>`;
});
html += `</span>\n`;
});
html = html.replace(/\n*$/, ""); // Get rid of final new lines
html += `</code></pre>`;
return html;
};
const makeHighlightSet = function (highlightLines) {
const lines = new Set();
if (!highlightLines) {
return lines;
}
for (let lineSpec of highlightLines) {
if (lineSpec.toString().includes("-")) {
const [begin, end] = lineSpec
.split("-")
.map((lineNo) => Number(lineNo));
for (let line = begin; line <= end; line++) {
lines.add(line);
}
} else if (lineSpec.toString().trim()) {
lines.add(Number(lineSpec));
}
}
return lines;
};
const htmlEscapes = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#39;",
};
function escapeHtml(html) {
return html.replace(/[&<>"']/g, (chr) => htmlEscapes[chr]);
}
exports.renderToHtml = renderToHtml;
@@ -0,0 +1,146 @@
const fs = require("fs");
const path = require("path");
const renderer = require("./renderer");
const args = JSON.parse(process.argv.slice(2));
const customLanguages = {
antlers: {
scopeName: "text.html.statamic",
embeddedLangs: ["html"],
},
};
async function main(args) {
const shiki = await import("shiki");
const highlighter = await shiki.createHighlighter({});
for (const [lang, spec] of Object.entries(customLanguages)) {
for (const embedded of spec.embeddedLangs) {
await highlighter.loadLanguage(embedded);
}
await highlighter.loadLanguage({
...spec,
...loadLanguage(lang),
name: lang,
});
}
const language = args[1] || "php";
/**
* If only one theme is provided, the variable `theme` will be a string. The variable `themes` will be null.
*
* If multiple themes are provided, the variable `themes` will be an array and the variable `theme` will be null.
*/
let theme = args[2] || "nord";
let themes = null;
if (typeof args[2] === "object") {
theme = null;
themes = args[2];
}
if (theme) {
if (fs.existsSync(theme)) {
theme = loadLocalTheme(theme);
} else {
await highlighter.loadTheme(theme);
}
} else if (themes) {
for (const theme of Object.values(themes)) {
if (fs.existsSync(theme)) {
themes[theme] = loadLocalTheme(theme);
} else {
await highlighter.loadTheme(theme);
}
}
}
if (!customLanguages[language]) await highlighter.loadLanguage(language);
if (args[0] === "languages") {
process.stdout.write(
JSON.stringify([
...Object.keys(shiki.bundledLanguagesBase),
...Object.keys(customLanguages),
])
);
return;
}
if (args[0] === "aliases") {
process.stdout.write(
JSON.stringify([
...Object.keys(shiki.bundledLanguages),
...Object.keys(customLanguages),
])
);
return;
}
if (args[0] === "themes") {
process.stdout.write(JSON.stringify(Object.keys(shiki.bundledThemes)));
return;
}
const codeToTokensOptions = {
lang: language,
};
if (theme) {
codeToTokensOptions.theme = theme;
} else if (themes) {
codeToTokensOptions.themes = themes;
}
const result = highlighter.codeToTokens(args[0], codeToTokensOptions);
const options = args[3] || {};
const renderToHtmlOptions = {
highlightLines: options.highlightLines,
addLines: options.addLines,
deleteLines: options.deleteLines,
focusLines: options.focusLines,
};
if (theme) {
renderToHtmlOptions.theme = highlighter.setTheme(theme);
} else if (themes) {
const themes$ = {};
for (const [theme, theme$] of Object.entries(themes)) {
themes$[theme] = highlighter.setTheme(theme$);
}
renderToHtmlOptions.themes = themes$;
}
const rendered = renderer.renderToHtml(result.tokens, renderToHtmlOptions);
process.stdout.write(rendered);
}
main(args);
function loadLanguage(language) {
const path = getLanguagePath(language);
const content = fs.readFileSync(path);
return JSON.parse(content);
}
function getLanguagePath(language) {
const url = path.join(
__dirname,
"..",
"languages",
`${language}.tmLanguage.json`
);
return path.normalize(url);
}
function loadLocalTheme(theme) {
return JSON.parse(fs.readFileSync(theme, "utf-8"));
}