Skip to content

Commit

Permalink
Highlight code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Aug 21, 2023
1 parent 85ea58f commit 6575381
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 12 deletions.
31 changes: 31 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
"remark-gfm": "^3.0.1",
"remark-parse": "^10.0.2",
"remark-rehype": "^10.1.0",
"shikiji": "^0.5.1",
"svelte": "^4.2.0",
"unified": "^11.0.1",
"unist-util-visit": "^5.0.0",
"vite": "^4.4.9"
}
}
50 changes: 50 additions & 0 deletions site/scripts/rehypeShikiji.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { visit, SKIP } from 'unist-util-visit'
import { codeToHast } from 'shikiji'

/**
* @returns {import('unified').Transformer}
*/
export function rehypeShikiji() {
return async (tree) => {
/** @type {typeof tree[]} */
const preNodes = []

// visit is sync only
visit(tree, async (node) => {
if (node.type === 'element' && node.tagName === 'pre') {
preNodes.push(node)
return SKIP
}
})

for (const node of preNodes) {
const codeNode = node.children?.[0]

const language = codeNode.properties?.className?.[0]?.replace(
'language-',
''
)
if (!language) return

const code = codeNode.children?.[0]?.value
if (!code) return

const codeHast = await codeToHast(code, {
lang: language,
themes: {
light: 'light-plus',
dark: 'dark-plus'
}
})

const preHast = codeHast.children[0]
// too bright
preHast.properties.style = preHast.properties.style.replace(
'background-color:#FFFFFF',
'background-color:#f3f0f0'
)

Object.assign(node, preHast)
}
}
}
2 changes: 2 additions & 0 deletions site/scripts/vitePluginMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import rehypeSlug from 'rehype-slug'
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
import rehypeStringify from 'rehype-stringify'
import { fileURLToPath } from 'node:url'
import { rehypeShikiji } from './rehypeShikiji.js'

/**
*
Expand All @@ -18,6 +19,7 @@ export function markdown() {
.use(remarkGfm)
.use(remarkRehype)
.use(rehypeSlug)
.use(rehypeShikiji)
.use(rehypeAutolinkHeadings, {
behavior: 'append',
properties: {
Expand Down
26 changes: 17 additions & 9 deletions site/src/components/PkgNode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@
>
{:else}
<span class="indentable inline-flex">
<span class="text-blue-700 @dark:text-blue-300 mr-[1ch] min-w-max"
>{keyText}</span
>
<span class="key mr-[1ch] min-w-max">{keyText}</span>
<span class="whitespace-nowrap token {typeof value}">
{JSON.stringify(value)}
</span>
Expand Down Expand Up @@ -93,41 +91,51 @@
</li>

<style>
/* NOTE: these styles must match shiki in the rules page */
.key {
color: #0451a5;
}
.token {
color: black;
}
.token.string {
color: brown;
color: #a31515;
}
.token.number {
color: yellow;
color: #098658;
}
.token.boolean {
color: blue;
color: #0000ff;
}
.indentable {
margin-left: var(--indent-ch);
}
@media (prefers-color-scheme: dark) {
.key {
color: #9cdcfe;
}
.token {
color: white;
}
.token.string {
color: burlywood;
color: #ce9178;
}
.token.number {
color: lightyellow;
color: #b5cea8;
}
.token.boolean {
color: lightblue;
color: #569cd6;
}
}
</style>
17 changes: 14 additions & 3 deletions site/src/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ body {
opacity: 0.8;
}

.docs code {
.docs :not(pre) code {
color: #121212;
background-color: #dcdcdc;
background-color: #cdcac9;
border-radius: 0.25rem;
padding: 0.15rem 0.25rem;
}
Expand All @@ -48,6 +48,11 @@ body {
padding: 0.5rem 0.8rem;
}

.docs .shiki {
border-radius: 0.25rem;
padding: 0.5rem;
}

.docs h2.active > code {
background-color: hsl(29, 74%, 77%);
}
Expand Down Expand Up @@ -204,7 +209,7 @@ button {
background-color: #2c2c2c;
}

.docs code {
.docs :not(pre) code {
color: #efefef;
background-color: #434343;
}
Expand All @@ -227,6 +232,12 @@ button {
color: #6fa6ff;
}

.shiki,
.shiki span {
background-color: var(--shiki-dark-bg) !important;
color: var(--shiki-dark) !important;
}

button {
background-color: #2c2c2c;
color: #efefef;
Expand Down

0 comments on commit 6575381

Please sign in to comment.