Skip to content

Commit b749d38

Browse files
committedJul 1, 2024
Add rehype-starry-night to website
1 parent e61be74 commit b749d38

File tree

3 files changed

+40
-102
lines changed

3 files changed

+40
-102
lines changed
 

‎package-lock.json

+37-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"rehype-remove-comments": "^6.0.0",
8282
"rehype-shift-heading": "^2.0.0",
8383
"rehype-slug": "^6.0.0",
84+
"rehype-starry-night": "^2.0.0",
8485
"rehype-stringify": "^10.0.0",
8586
"remark-cli": "^12.0.0",
8687
"remark-directive": "^3.0.0",

‎website/mdx-config.js

+2-99
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
22
* @import {CompileOptions} from '@mdx-js/mdx'
3-
* @import {Grammar} from '@wooorm/starry-night'
43
* @import {Program} from 'estree'
54
* @import {ElementContent, Root} from 'hast'
65
* @import {VFile} from 'vfile'
@@ -17,12 +16,11 @@
1716

1817
import {pathToFileURL} from 'node:url'
1918
import {nodeTypes} from '@mdx-js/mdx'
20-
import {common, createStarryNight} from '@wooorm/starry-night'
19+
import {common} from '@wooorm/starry-night'
2120
import sourceMdx from '@wooorm/starry-night/source.mdx'
2221
import sourceTsx from '@wooorm/starry-night/source.tsx'
2322
import {valueToEstree} from 'estree-util-value-to-estree'
2423
import {h, s} from 'hastscript'
25-
import {toString} from 'hast-util-to-string'
2624
import {toText} from 'hast-util-to-text'
2725
import {analyze} from 'periscopic'
2826
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
@@ -32,6 +30,7 @@ import rehypeInferTitleMeta from 'rehype-infer-title-meta'
3230
import rehypeMinifyUrl from 'rehype-minify-url'
3331
import rehypeShiftHeading from 'rehype-shift-heading'
3432
import rehypeSlug from 'rehype-slug'
33+
import rehypeStarryNight from 'rehype-starry-night'
3534
import rehypePresetMinify from 'rehype-preset-minify'
3635
import rehypeRaw from 'rehype-raw'
3736
import remarkFrontmatter from 'remark-frontmatter'
@@ -324,99 +323,3 @@ function rehypePrettyCodeBlocks() {
324323
})
325324
}
326325
}
327-
328-
// See:
329-
// <https://github.com/wooorm/starry-night#example-integrate-with-unified-remark-and-rehype>.
330-
/**
331-
* @typedef Options
332-
* Configuration (optional)
333-
* @property {Array<Grammar> | null | undefined} [grammars]
334-
* Grammars to support (default: `common`).
335-
*/
336-
337-
/**
338-
* Highlight code with `starry-night`.
339-
*
340-
* @param {Readonly<Options> | null | undefined} options
341-
* Configuration (optional).
342-
* @returns
343-
* Transform.
344-
*/
345-
function rehypeStarryNight(options) {
346-
const settings = options || {}
347-
const grammars = settings.grammars || common
348-
const starryNightPromise = createStarryNight(grammars)
349-
const prefix = 'language-'
350-
351-
/**
352-
* Transform.
353-
*
354-
* @param {Root} tree
355-
* Tree.
356-
* @returns {Promise<undefined>}
357-
* Nothing.
358-
*/
359-
return async function (tree) {
360-
const starryNight = await starryNightPromise
361-
362-
visit(tree, 'element', function (node, index, parent) {
363-
if (!parent || typeof index !== 'number' || node.tagName !== 'pre') {
364-
return
365-
}
366-
367-
const head = node.children[0]
368-
369-
if (
370-
!head ||
371-
head.type !== 'element' ||
372-
head.tagName !== 'code' ||
373-
!head.properties
374-
) {
375-
return
376-
}
377-
378-
const classes = head.properties.className
379-
380-
if (!Array.isArray(classes)) return
381-
382-
const language = classes.find(function (d) {
383-
return typeof d === 'string' && d.startsWith(prefix)
384-
})
385-
386-
if (typeof language !== 'string') return
387-
388-
const name = language.slice(prefix.length)
389-
390-
const scope = starryNight.flagToScope(name)
391-
392-
// Maybe warn?
393-
if (!scope) {
394-
if (name !== 'txt') console.warn('Missing language: %s', name)
395-
return
396-
}
397-
398-
const fragment = starryNight.highlight(toString(head), scope)
399-
// Cast because we don’t expect doctypes.
400-
const children = /** @type {Array<ElementContent>} */ (fragment.children)
401-
402-
parent.children.splice(index, 1, {
403-
type: 'element',
404-
tagName: 'pre',
405-
properties: {},
406-
children: [
407-
{
408-
type: 'element',
409-
tagName: 'code',
410-
properties: {
411-
className: [
412-
'language-' +
413-
scope.replace(/^source\./, '').replaceAll('.', '-')
414-
]
415-
},
416-
children
417-
}
418-
]
419-
})
420-
})
421-
}
422-
}

0 commit comments

Comments
 (0)
Please sign in to comment.