-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_config.ts
More file actions
180 lines (159 loc) · 4.31 KB
/
_config.ts
File metadata and controls
180 lines (159 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import Lua from "@tree-sitter-grammars/tree-sitter-lua"
import Markdown from "@tree-sitter-grammars/tree-sitter-markdown"
import YAML from "@tree-sitter-grammars/tree-sitter-yaml"
import cssnano from "cssnano"
import advancedPreset from "cssnano-preset-advanced"
import escapeHtml from "escape-html"
import lume from "lume/mod.ts"
import googleFonts from "lume/plugins/google_fonts.ts"
import jsx from "lume/plugins/jsx.ts"
import mdx from "lume/plugins/mdx.ts"
import metas from "lume/plugins/metas.ts"
import minifyHTML from "lume/plugins/minify_html.ts"
import multilanguage from "lume/plugins/multilanguage.ts"
import postcss from "lume/plugins/postcss.ts"
import redirects from "lume/plugins/redirects.ts"
import robots from "lume/plugins/robots.ts"
import sitemap from "lume/plugins/sitemap.ts"
import transformImages from "lume/plugins/transform_images.ts"
import variableCompress from "postcss-variable-compress"
import rehypeExternalLinks from "rehype-external-links"
import rehypeRaw from "rehype-raw"
import Parser, { type Language } from "tree-sitter"
import Bash from "tree-sitter-bash"
import Go from "tree-sitter-go"
import HTML from "tree-sitter-html"
import JavaScript from "tree-sitter-javascript"
import JSON from "tree-sitter-json"
import TypeScript from "tree-sitter-typescript"
import tailwindcss from "./src/_plugins/tailwindcss.ts"
const site = lume({
dest: "./public",
src: "./src",
location: new URL("https://akimo.dev"),
})
const parseCode = (code: string, lang: string) => {
const languages = {
go: Go,
html: HTML,
javascript: JavaScript,
json: JSON,
lua: Lua,
markdown: Markdown,
shell: Bash,
typescript: TypeScript.typescript,
yaml: YAML,
} as Record<string, Language>
if (!(lang in languages)) {
return `<pre><code>${escapeHtml(code)}</code></pre>`
}
const parser = new Parser()
parser.setLanguage(languages[lang])
const tree = parser.parse(code)
const wrapTag = (node: Parser.SyntaxNode, content?: string): string => {
let openTag = `<span class="ts-${escapeHtml(node.type)}">`,
closeTag = "</span>"
if (node.parent === null) {
openTag = `<pre><code class="lang-${lang}">`
closeTag = "</code></pre>"
}
return openTag + (content || escapeHtml(node.text)) + closeTag
}
const nodeToHtml = (node: Parser.SyntaxNode): string => {
if (node.childCount === 0) {
return wrapTag(node)
}
let content = ""
let lastIndex = node.startIndex
for (const child of node.children) {
content += escapeHtml(
node.text.slice(
lastIndex - node.startIndex,
child.startIndex - node.startIndex,
),
)
content += nodeToHtml(child)
lastIndex = child.endIndex
}
content += escapeHtml(node.text.slice(lastIndex - node.startIndex))
return wrapTag(node, content)
}
return nodeToHtml(tree.rootNode)
}
site.copy("icon")
site.use(googleFonts({
cssFile: "style.css",
fonts:
"https://fonts.google.com/share?selection.family=Atkinson+Hyperlegible:ital,wght@0,400;0,700;1,400;1,700",
}))
site.use(jsx())
interface MdastNode {
lang?: string
value: string
}
site.use(mdx({
rehypeOptions: {
allowDangerousHtml: true,
handlers: {
code: (_: unknown, node: MdastNode) => {
return {
type: "raw",
value: parseCode(node.value, node.lang ?? ""),
}
},
},
},
rehypePlugins: [
[
rehypeExternalLinks,
{
properties: {
class:
"after:content-open-in-new after:dark:content-open-in-new-dark",
},
rel: ["noopener", "noreferrer"],
target: "_blank",
},
],
[
rehypeRaw,
{
passThrough: ["mdxJsxFlowElement", "mdxJsxTextElement"],
},
],
],
}))
site.use(redirects())
site.use(tailwindcss())
site.use(postcss({
plugins: [
cssnano({
preset: advancedPreset({
discardComments: {
removeAll: true,
},
}),
}),
variableCompress,
],
}))
site.use(robots({
rules: [
{
userAgent: "*",
disallow: "/external/",
},
],
}))
site.use(transformImages())
site.add("/img")
site.use(multilanguage({
defaultLanguage: "ja",
languages: ["ja", "en"],
}))
site.use(sitemap({
query: "externalUrl=undefined",
}))
site.use(metas())
site.use(minifyHTML())
export default site