-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use @wooorm/starry-night for syntax highlighting (#143)
Use the high-quality syntax highlighting provided by @wooorm/starry-night. Now it matches GitHub perfectly. ## Before <img width="661" alt="" src="https://github.com/stevenpetryk/mafs/assets/1724000/e8d8fdca-180f-407f-bb0e-9bc5cf32a937"> ## After <img width="660" alt="" src="https://github.com/stevenpetryk/mafs/assets/1724000/e27cb4e6-f3e6-496b-ae51-f0b20f256875">
- Loading branch information
1 parent
2725a0a
commit c9ed792
Showing
8 changed files
with
514 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// @ts-check | ||
/* eslint-disable no-undef, @typescript-eslint/no-var-requires */ | ||
|
||
import { createStarryNight, all } from "@wooorm/starry-night" | ||
|
||
const starryNightPromise = createStarryNight(all) | ||
|
||
/** | ||
* This loader is what powers Mafs' guide examples. It takes the default export | ||
* of all the code in components/guide-examples and makes sure that the source | ||
* code is also exported, and also takes care of syntax highlighting | ||
* at build time. | ||
* | ||
* Since all the guide-examples are maintained in this repo, I didn't bother | ||
* bringing in a proper parser. I just find and replace the default export. | ||
* | ||
* @type {import('webpack').LoaderDefinition} | ||
*/ | ||
export default async function guideExampleLoader(source) { | ||
let cleanedSource = source | ||
|
||
const remove = [ | ||
/\s+height=\{[^}]*\}/g, | ||
/\s+width=\{.*\}\s*/g, | ||
/.*prettier-ignore.*\n/gm, | ||
/^import React.* from "react"/gm, | ||
/^export default [A-z]+$\n/gm, | ||
/^export default /m, | ||
/"use client"/m, | ||
] | ||
|
||
remove.forEach((regex) => { | ||
cleanedSource = cleanedSource.replace(regex, "") | ||
}) | ||
|
||
cleanedSource = cleanedSource.replaceAll(/import \{(.|\n)*?\} from "mafs"/gm, (match) => { | ||
return match.replaceAll(/\s+/g, " ").replace(", }", " }") | ||
}) | ||
|
||
cleanedSource = cleanedSource.trim() | ||
|
||
const starryNight = await starryNightPromise | ||
const scope = starryNight.flagToScope(this.resourcePath) | ||
const tree = starryNight.highlight(cleanedSource, scope ?? "tsx") | ||
|
||
const useClientRegex = /'use client'|"use client"/ | ||
const usesClient = useClientRegex.test(source) | ||
|
||
const transformedSource = source | ||
.replace(/export default/g, "const $default =") | ||
.replace(useClientRegex, "") | ||
|
||
return ` | ||
${usesClient ? "'use client';" : ""} | ||
import {Fragment, jsx, jsxs} from 'react/jsx-runtime'; | ||
import {toJsxRuntime} from 'hast-util-to-jsx-runtime'; | ||
${transformedSource}; | ||
$default.$source = ${JSON.stringify(cleanedSource)}; | ||
$default.$component = $default; | ||
$default.$highlightedSource = toJsxRuntime(${JSON.stringify(tree)}, { Fragment, jsx, jsxs }); | ||
export default $default; | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.