Skip to content

Commit

Permalink
Cleanup, improve article parsing, add terminal grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
shiro committed Apr 24, 2024
1 parent 416ae9e commit 9574e2e
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
app.config.timestamp*
input/
generated/
*.bundled_*.mjs
5 changes: 3 additions & 2 deletions app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import remarkGfm from "remark-gfm";
import { parseDelimitedString } from "./src/util/parseDelimitedString";
import tsconfig from "./tsconfig.json";
// import { ssrBabelPlugin } from "./vite/ssrBabelPlugin";
// @ts-ignore
import SSPreloadBabel from "solid-start-preload/babel";
import { viteImagePlugin } from "./vite/viteImagePlugin";
import { viteMDPlugin } from "./vite/viteMDPlugin";
import { viteMarkdownPlugin } from "./vite/markdown/viteMarkdownPlugin";

// import devtools from "solid-devtools/vite";

Expand Down Expand Up @@ -78,7 +79,7 @@ export default defineConfig({
viteImagePlugin(),
compileTime(),
solidSvg(),
viteMDPlugin(),
viteMarkdownPlugin(),
// devtools({
// autoname: true,
// locator: {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"node": ">=18"
},
"devDependencies": {
"shiki": "1.3.0",
"@shikijs/core": "1.3.0",
"@shikijs/rehype": "1.3.0",
"@shikijs/transformers": "1.3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
title: Deploying NixOS to Digital Ocean
private: false
---

import DialogImage from "~/DialogImage";

import NoNixOSImg from "./no-nixos.jpg?lazy";
Expand Down Expand Up @@ -137,7 +136,7 @@ the new packages we specified.
After switching, the packages should be available as globally installed
system packages.

```txt
```terminal colors
[nix-shell:/etc/nixos]# exit
[root@droplet:~]# nixos-rebuild switch
[root@droplet:~]# echo "NixOS in the cloud!" | cowsay
Expand Down
14 changes: 12 additions & 2 deletions src/ssg/getArticles.ssg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@ export const getArticlesSSG = () => {
const raw = fs
.readFileSync(path.join(base, slug, "article.mdx"))
.toString();
const { data: frontmatter, content } = matter(raw);
let { data: frontmatter, content } = matter(raw);
if (frontmatter.private) return;

const title = frontmatter.title;
if (!title) return;

const description = content.match(/\n\n\n([\s\S]+?)(?=\n\n)/)?.[1];
// trim import statements and empty lines
content = (() => {
const c = content.split("\n");
while (c.length && (!c[0] || c[0].startsWith("import "))) {
c.splice(0, 1);
}
console.log(c[0]);
return c.join("\n");
})();

const description = content.match(/([\s\S]+?)(?=\n\n)/)?.[1];

const url = `/articles/${slug.split(".")[0]}`;
const date = slug.split("-").slice(0, 3).reverse().join(".");
Expand Down
9 changes: 9 additions & 0 deletions src/style/global.style.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,15 @@ export const globals = css`
.language-id {
display: none;
}
.color-red {
color: #f97583 !important;
}
.color-blue {
color: rgb(158, 203, 255) !important;
}
.color-gray {
color: ${color("colors/text-300a")} !important;
}
&.diff {
.line {
&::before {
Expand Down
68 changes: 68 additions & 0 deletions vite/markdown/shikiColorNotation/shikiColorNotation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { ShikiTransformer } from "@shikijs/core";
import type { Element } from "hast";

export interface shikiColorNotationOptions {
colors?: string[];
}

type MetaNode = Element & { meta?: Record<string, any> };

export function shikiColorNotation(
options: shikiColorNotationOptions = {}
): ShikiTransformer {
const { colors = ["red", "blue", "green", "orange", "gray"] } = options;

return {
name: "shiki-color-notation",
code(node: MetaNode) {
if (!node.meta?.colors) return;

const lines = node.children.filter(
(node) => node.type === "element"
) as Element[];

lines.forEach((line) => {
for (let i = 0; i < line.children.length; i++) {
const child = line.children[i];

if (child.type !== "element") continue;
const text = child.children[0];
if (text.type !== "text") continue;

const m = /(.*)%(.+)%(.*)%\2%(.*)/.exec(text.value);

if (!m) continue;

const [raw, before, color, inner, after] = m;

if (colors.includes(color)) {
text.value = inner;

if (inner) {
child.properties.class = `color-${color}`;
}

if (after) {
line.children.splice(i + 1, 0, {
type: "element",
tagName: "span",
properties: {},
children: [{ type: "text", value: after }],
});
}

if (before) {
line.children.splice(i, 0, {
type: "element",
tagName: "span",
properties: {},
children: [{ type: "text", value: before }],
});
i--;
}
}
}
});
},
};
}
File renamed without changes.
24 changes: 24 additions & 0 deletions vite/markdown/shikiGrammars/terminal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"displayName": "Terminal",
"name": "terminal",
"patterns": [
{
"include": "#prompt"
}
],
"repository": {
"prompt": {
"begin": "^\\[",
"end": "\\]# ([^ ]+)",
"beginCaptures": {
"0": { "name": "string" }
},
"endCaptures": {
"0": { "name": "string" },
"1": { "name": "variable" }
},
"name": "variable"
}
},
"scopeName": "text.colors"
}
22 changes: 19 additions & 3 deletions vite/viteMDPlugin.ts → vite/markdown/viteMarkdownPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { nodeTypes } from "@mdx-js/mdx";
import fs from "fs";
import rehypeRaw from "rehype-raw";
import remarkFrontmatter from "remark-frontmatter";
// @ts-ignore
Expand All @@ -8,12 +9,14 @@ import _mdx from "@vinxi/plugin-mdx";
// @ts-ignore
import remarkCaptions from "remark-captions";
import remarkGfm from "remark-gfm";
import { parseDelimitedString } from "../src/util/parseDelimitedString";
import { shikiDiffNotation } from "../ext/shikiDiffNotation/shikiDiffNotation";
import { bundledLanguages } from "shiki";
import { parseDelimitedString } from "../../src/util/parseDelimitedString";
import { shikiColorNotation } from "./shikiColorNotation/shikiColorNotation";
import { shikiDiffNotation } from "./shikiDiffNotation/shikiDiffNotation";

const { default: mdx } = _mdx;

export const viteMDPlugin = () =>
export const viteMarkdownPlugin = () =>
mdx.withImports({})({
jsx: true,
jsxImportSource: "solid-js",
Expand All @@ -28,6 +31,18 @@ export const viteMDPlugin = () =>
rehypeShiki,
{
theme: "github-dark",
langs: [
...Object.keys(bundledLanguages),
// theme colors: https://github.com/shikijs/textmate-grammars-themes/blob/45c05724db7ce7015e81d68b5b3f56dfcc0e8a2b/packages/tm-themes/themes/github-dark.json
JSON.parse(
fs
.readFileSync(
"vite/markdown/shikiGrammars/terminal.json",
"utf-8"
)
.toString()
),
],
transformers: [
// codeblock meta parser
(() => {
Expand All @@ -50,6 +65,7 @@ export const viteMDPlugin = () =>
};
})(),
shikiDiffNotation(),
shikiColorNotation(),
],
},
],
Expand Down

0 comments on commit 9574e2e

Please sign in to comment.