1
1
/**
2
2
* @import {CompileOptions} from '@mdx-js/mdx'
3
- * @import {Grammar} from '@wooorm/starry-night'
4
3
* @import {Program} from 'estree'
5
4
* @import {ElementContent, Root} from 'hast'
6
5
* @import {VFile} from 'vfile'
17
16
18
17
import { pathToFileURL } from 'node:url'
19
18
import { nodeTypes } from '@mdx-js/mdx'
20
- import { common , createStarryNight } from '@wooorm/starry-night'
19
+ import { common } from '@wooorm/starry-night'
21
20
import sourceMdx from '@wooorm/starry-night/source.mdx'
22
21
import sourceTsx from '@wooorm/starry-night/source.tsx'
23
22
import { valueToEstree } from 'estree-util-value-to-estree'
24
23
import { h , s } from 'hastscript'
25
- import { toString } from 'hast-util-to-string'
26
24
import { toText } from 'hast-util-to-text'
27
25
import { analyze } from 'periscopic'
28
26
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
@@ -32,6 +30,7 @@ import rehypeInferTitleMeta from 'rehype-infer-title-meta'
32
30
import rehypeMinifyUrl from 'rehype-minify-url'
33
31
import rehypeShiftHeading from 'rehype-shift-heading'
34
32
import rehypeSlug from 'rehype-slug'
33
+ import rehypeStarryNight from 'rehype-starry-night'
35
34
import rehypePresetMinify from 'rehype-preset-minify'
36
35
import rehypeRaw from 'rehype-raw'
37
36
import remarkFrontmatter from 'remark-frontmatter'
@@ -324,99 +323,3 @@ function rehypePrettyCodeBlocks() {
324
323
} )
325
324
}
326
325
}
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 ( / ^ s o u r c e \. / , '' ) . replaceAll ( '.' , '-' )
414
- ]
415
- } ,
416
- children
417
- }
418
- ]
419
- } )
420
- } )
421
- }
422
- }
0 commit comments