初始提交:时之刃的小站

基于 VermilionVoid(朱墨留白)主题的个人博客。
- 站点配置集中在 src/data/site.ts 与 src/data/profile.ts
- 30 篇文章、8 条偶得、相册
- 评论使用自托管 twikoo
- 部署走 deploy.sh(rsync 到树莓派 + 云服务器)
This commit is contained in:
dsh
2026-09-14 20:44:12 +08:00
commit 6f4cc13a20
209 changed files with 36176 additions and 0 deletions
@@ -0,0 +1,97 @@
import { definePlugin } from "@expressive-code/core";
type AstNode = {
type: string;
tagName?: string;
properties?: Record<string, unknown>;
children?: AstNode[];
};
export function pluginCustomCopyButton() {
return definePlugin({
name: "Custom Copy Button",
hooks: {
postprocessRenderedBlock: (context) => {
function traverse(node: AstNode) {
if (node.type === "element" && node.tagName === "pre") {
processCodeBlock(node);
return;
}
if (node.children) {
for (const child of node.children) {
if (child.type === "element") traverse(child);
}
}
}
function processCodeBlock(node: AstNode) {
const copyButton = {
type: "element" as const,
tagName: "button",
properties: {
className: ["copy-btn"],
"aria-label": "Copy code",
},
children: [
{
type: "element" as const,
tagName: "div",
properties: {
className: ["copy-btn-icon"],
},
children: [
{
type: "element" as const,
tagName: "svg",
properties: {
viewBox: "0 -960 960 960",
xmlns: "http://www.w3.org/2000/svg",
className: ["copy-btn-icon", "copy-icon"],
},
children: [
{
type: "element" as const,
tagName: "path",
properties: {
d: "M368.37-237.37q-34.48 0-58.74-24.26-24.26-24.26-24.26-58.74v-474.26q0-34.48 24.26-58.74 24.26-24.26 58.74-24.26h378.26q34.48 0 58.74 24.26 24.26 24.26 24.26 58.74v474.26q0 34.48-24.26 58.74-24.26 24.26-58.74 24.26H368.37Zm0-83h378.26v-474.26H368.37v474.26Zm-155 238q-34.48 0-58.74-24.26-24.26-24.26-24.26-58.74v-515.76q0-17.45 11.96-29.48 11.97-12.02 29.33-12.02t29.54 12.02q12.17 12.03 12.17 29.48v515.76h419.76q17.45 0 29.48 11.96 12.02 11.97 12.02 29.33t-12.02 29.54q-12.03 12.17-29.48 12.17H213.37Zm155-238v-474.26 474.26Z",
},
children: [],
},
],
},
{
type: "element" as const,
tagName: "svg",
properties: {
viewBox: "0 -960 960 960",
xmlns: "http://www.w3.org/2000/svg",
className: ["copy-btn-icon", "success-icon"],
},
children: [
{
type: "element" as const,
tagName: "path",
properties: {
d: "m389-377.13 294.7-294.7q12.58-12.67 29.52-12.67 16.93 0 29.61 12.67 12.67 12.68 12.67 29.53 0 16.86-12.28 29.14L419.07-288.41q-12.59 12.67-29.52 12.67-16.94 0-29.62-12.67L217.41-430.93q-12.67-12.68-12.79-29.45-.12-16.77 12.55-29.45 12.68-12.67 29.62-12.67 16.93 0 29.28 12.67L389-377.13Z",
},
children: [],
},
],
},
],
},
],
} as AstNode;
if (!node.children) {
node.children = [];
}
node.children.push(copyButton);
}
const blockAst = context.renderData.blockAst as AstNode;
traverse(blockAst);
},
},
});
}
@@ -0,0 +1,49 @@
/**
* Based on the discussion at https://github.com/expressive-code/expressive-code/issues/153#issuecomment-2282218684
*/
import { definePlugin } from "@expressive-code/core";
export function pluginLanguageBadge() {
return definePlugin({
name: "Language Badge",
baseStyles: () => `
[data-language]::before {
position: absolute;
z-index: 2;
right: 0.5rem;
top: 0.5rem;
padding: 0.1rem 0.5rem;
content: attr(data-language);
font-family: var(--font-mono), ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
font-size: 0.75rem;
font-weight: bold;
text-transform: uppercase;
color: var(--muted-foreground);
background: var(--muted);
border-radius: 0.5rem;
pointer-events: none;
transition: opacity 0.3s;
opacity: 0;
}
.frame:not(.has-title):not(.is-terminal) {
@media (hover: none) {
& [data-language]::before {
opacity: 1;
margin-right: 3rem;
}
& [data-language]:active::before {
opacity: 0;
}
}
@media (hover: hover) {
& [data-language]::before {
opacity: 1;
}
&:hover [data-language]::before {
opacity: 0;
}
}
}
`,
});
}
@@ -0,0 +1,34 @@
// @ts-nocheck
/// <reference types="mdast" />
import { h } from "hastscript";
/**
* Creates an admonition component.
*
* @param {Object} properties - The properties of the component.
* @param {string} [properties.title] - An optional title.
* @param {('tip'|'note'|'important'|'caution'|'warning')} type - The admonition type.
* @param {import('mdast').RootContent[]} children - The children elements of the component.
* @returns {import('mdast').Parent} The created admonition component.
*/
export function AdmonitionComponent(properties, children, type) {
if (!Array.isArray(children) || children.length === 0)
return h(
"div",
{ class: "hidden" },
'Invalid admonition directive. (Admonition directives must be of block type ":::note{name="name"} <content> :::")',
);
let label = null;
if (properties?.["has-directive-label"] || properties?.hasDirectiveLabel) {
label = children[0]; // The first child is the label
// biome-ignore lint/style/noParameterAssign: <check later>
children = children.slice(1);
label.tagName = "div"; // Change the tag <p> to <div>
}
return h("blockquote", { class: `admonition bdm-${type}` }, [
h("span", { class: "bdm-title" }, label ? label : type.toUpperCase()),
...children,
]);
}
@@ -0,0 +1,165 @@
/// <reference types="mdast" />
// @ts-nocheck
import { h } from "hastscript";
// GitHub language colors (subset of most common languages)
const LANGUAGE_COLORS = {
JavaScript: "#f1e05a",
TypeScript: "#3178c6",
Python: "#3572A5",
Java: "#b07219",
"C++": "#f34b7d",
C: "#555555",
"C#": "#178600",
Go: "#00ADD8",
Rust: "#dea584",
Ruby: "#701516",
PHP: "#4F5D95",
Swift: "#F05138",
Kotlin: "#A97BFF",
Dart: "#00B4AB",
Scala: "#c22d40",
Shell: "#89e051",
HTML: "#e34c26",
CSS: "#563d7c",
SCSS: "#c6538c",
Vue: "#41b883",
Svelte: "#ff3e00",
Astro: "#ff5a03",
MDX: "#fcb32c",
Markdown: "#083fa1",
JSON: "#292929",
YAML: "#cb171e",
Dockerfile: "#384d54",
Makefile: "#427819",
Lua: "#000080",
Perl: "#0298c3",
R: "#198CE7",
Julia: "#a270ba",
Haskell: "#5e5086",
Elixir: "#6e4a7e",
Clojure: "#db5855",
Objective_C: "#438eff",
"Objective-C": "#438eff",
Jupyter_Notebook: "#DA5B0B",
"Jupyter Notebook": "#DA5B0B",
Vim_Script: "#199f4b",
TeX: "#3D6117",
PowerShell: "#012456",
};
/**
* Creates a GitHub Card component.
*
* @param {Object} properties - The properties of the component.
* @param {string} properties.repo - The GitHub repository in the format "owner/repo".
* @param {import('mdast').RootContent[]} children - The children elements of the component.
* @returns {import('mdast').Parent} The created GitHub Card component.
*/
export function GithubCardComponent(properties, children) {
if (Array.isArray(children) && children.length !== 0)
return h("div", { class: "hidden" }, [
'Invalid directive. ("github" directive must be leaf type "::github{repo="owner/repo"}")',
]);
if (!properties.repo || !properties.repo.includes("/"))
return h(
"div",
{ class: "hidden" },
'Invalid repository. ("repo" attributte must be in the format "owner/repo")',
);
const repo = properties.repo;
const cardUuid = `GC${Math.random().toString(36).slice(-6)}`; // Collisions are not important
const nAvatar = h(`div#${cardUuid}-avatar`, { class: "gc-avatar" });
const nLanguage = h(
`span#${cardUuid}-language`,
{ class: "gc-language" },
"—",
);
const nTitle = h("div", { class: "gc-titlebar" }, [
h("div", { class: "gc-titlebar-left" }, [
h("div", { class: "gc-owner" }, [
nAvatar,
h("div", { class: "gc-user" }, repo.split("/")[0]),
]),
h("div", { class: "gc-divider" }, "/"),
h("div", { class: "gc-repo" }, repo.split("/")[1]),
]),
h("div", { class: "github-logo" }),
]);
const nDescription = h(
`div#${cardUuid}-description`,
{ class: "gc-description" },
"Loading...",
);
const nStars = h(`div#${cardUuid}-stars`, { class: "gc-stars" }, "—");
const nForks = h(`div#${cardUuid}-forks`, { class: "gc-forks" }, "—");
const nLicense = h(`div#${cardUuid}-license`, { class: "gc-license" }, "—");
const nScript = h(
`script#${cardUuid}-script`,
{ type: "text/javascript", defer: true, "data-astro-rerun": true },
`
(function() {
const __gcLangColorsMap = ${JSON.stringify(LANGUAGE_COLORS)};
const debugEnabled = () => localStorage.getItem("debugGithubCard") === "1";
const debug = (...args) => { if (debugEnabled()) console.log("[GITHUB-CARD]", ...args); };
const cardEl = document.getElementById('${cardUuid}-card');
if (cardEl && cardEl.dataset.githubLoaded === "true") {
// already loaded by fallback loader
debug("skip (already loaded)", "${repo}");
return;
}
if (cardEl) cardEl.dataset.githubLoaded = "loading";
debug("inline fetch", "${repo}");
fetch('https://api.github.com/repos/${repo}', { referrerPolicy: "no-referrer" }).then(response => response.json()).then(data => {
document.getElementById('${cardUuid}-description').innerText = data.description?.replace(/:[a-zA-Z0-9_]+:/g, '') || "No description provided";
const lang = data.language || "";
const langEl = document.getElementById('${cardUuid}-language');
langEl.innerText = lang || "—";
if (lang && __gcLangColorsMap[lang]) {
langEl.style.setProperty('--gc-lang-color', __gcLangColorsMap[lang]);
}
document.getElementById('${cardUuid}-forks').innerText = Intl.NumberFormat('en-us', { notation: "compact", maximumFractionDigits: 1 }).format(data.forks).replaceAll("\u202f", '');
document.getElementById('${cardUuid}-stars').innerText = Intl.NumberFormat('en-us', { notation: "compact", maximumFractionDigits: 1 }).format(data.stargazers_count).replaceAll("\u202f", '');
const avatarEl = document.getElementById('${cardUuid}-avatar');
avatarEl.style.backgroundImage = 'url(' + data.owner.avatar_url + ')';
avatarEl.style.backgroundColor = 'transparent';
document.getElementById('${cardUuid}-license').innerText = data.license?.spdx_id || "—";
document.getElementById('${cardUuid}-card').classList.remove("fetch-waiting");
if (cardEl) cardEl.dataset.githubLoaded = "true";
debug("inline loaded", "${repo}");
}).catch(() => {
const c = document.getElementById('${cardUuid}-card');
c?.classList.add("fetch-error");
if (cardEl) cardEl.dataset.githubLoaded = "error";
console.warn("[GITHUB-CARD] Error loading card for ${repo}");
});
})();
`,
);
return h(
`a#${cardUuid}-card`,
{
class: "card-github fetch-waiting not-prose",
href: `https://github.com/${repo}`,
target: "_blank",
rel: "noopener noreferrer",
"data-github-card": "",
"data-github-repo": repo,
repo,
},
[
nTitle,
nDescription,
h("div", { class: "gc-infobar" }, [nStars, nForks, nLicense, nLanguage]),
nScript,
],
);
}
+33
View File
@@ -0,0 +1,33 @@
// @ts-nocheck
import { h } from "hastscript";
import { visit } from "unist-util-visit";
/**
* @returns {import('unified').Transformer<import('mdast').Root>}
*/
export function parseDirectiveNode() {
return (tree) => {
visit(tree, (node) => {
if (
node.type === "containerDirective" ||
node.type === "leafDirective" ||
node.type === "textDirective"
) {
// biome-ignore lint/suspicious/noAssignInExpressions: <check later>
const data = node.data || (node.data = {});
node.attributes = node.attributes || {};
if (
node.children.length > 0 &&
node.children[0].data &&
node.children[0].data.directiveLabel
) {
node.attributes["has-directive-label"] = true;
}
const hast = h(node.name, node.attributes);
data.hName = hast.tagName;
data.hProperties = hast.properties;
}
});
};
}
+18
View File
@@ -0,0 +1,18 @@
// @ts-nocheck
// biome-ignore lint/suspicious/noShadowRestrictedNames: <toString from mdast-util-to-string>
import { toString } from "mdast-util-to-string";
/* Use the post's first paragraph as the excerpt */
export function remarkExcerpt() {
return (tree, { data }) => {
let excerpt = "";
for (const node of tree.children) {
if (node.type !== "paragraph") {
continue;
}
excerpt = toString(node);
break;
}
data.astro.frontmatter.excerpt = excerpt;
};
}
+16
View File
@@ -0,0 +1,16 @@
// @ts-nocheck
// biome-ignore lint/suspicious/noShadowRestrictedNames: <toString from mdast-util-to-string>
import { toString } from "mdast-util-to-string";
import getReadingTime from "reading-time";
export function remarkReadingTime() {
return (tree, { data }) => {
const textOnPage = toString(tree);
const readingTime = getReadingTime(textOnPage);
data.astro.frontmatter.minutes = Math.max(
1,
Math.round(readingTime.minutes),
);
data.astro.frontmatter.words = readingTime.words;
};
}
+21
View File
@@ -0,0 +1,21 @@
import { visit } from "unist-util-visit"
export function remarkShiftHeadings(options = {}) {
const {
shift = 1,
onlyIn = ["/src/content/posts/"]
} = options
return (tree, file) => {
const rawPath = typeof file?.path === "string" ? file.path : ""
const normalizedPath = rawPath.replace(/\\/g, "/")
const shouldShift = onlyIn.some((segment) => normalizedPath.includes(segment))
if (!shouldShift || shift <= 0) return
visit(tree, "heading", (node) => {
if (typeof node.depth !== "number") return
node.depth = Math.min(node.depth + shift, 6)
})
}
}