-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from huntabyte/docs
- Loading branch information
Showing
104 changed files
with
2,509 additions
and
218 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@huntabyte/primitives": patch | ||
--- | ||
|
||
- Update button events |
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,138 @@ | ||
import remarkGfm from "remark-gfm"; | ||
import rehypePrettyCode from "rehype-pretty-code"; | ||
import { fileURLToPath } from "url"; | ||
import { getHighlighter } from "shikiji"; | ||
import { toHtml } from "hast-util-to-html"; | ||
import { visit } from "unist-util-visit"; | ||
import { escapeSvelte } from "@huntabyte/mdsvex"; | ||
import { resolve } from "path"; | ||
|
||
const __dirname = fileURLToPath(new URL(".", import.meta.url)); | ||
|
||
/** | ||
* @type {import('rehype-pretty-code').Options} | ||
*/ | ||
const prettyCodeOptions = { | ||
theme: "github-dark", | ||
keepBackground: false, | ||
onVisitLine(node) { | ||
if (node.children.length === 0) { | ||
node.children = { type: "text", value: " " }; | ||
} | ||
}, | ||
onVisitHighlightedLine(node) { | ||
node.properties.className = ["line--highlighted"]; | ||
}, | ||
onVisitHighlightedWord(node) { | ||
node.properties.className = ["word--highlighted"]; | ||
}, | ||
getHighlighter: (options) => { | ||
return getHighlighter({ | ||
...options, | ||
langs: ["typescript", "javascript", "bash", "json", "svelte", "css"] | ||
}); | ||
} | ||
}; | ||
|
||
/** @type {import('@huntabyte/mdsvex').MdsvexOptions} */ | ||
export const mdsvexOptions = { | ||
extensions: [".md"], | ||
layout: resolve(__dirname, "./src/components/markdown/layout.svelte"), | ||
smartypants: { | ||
quotes: false, | ||
ellipses: false, | ||
backticks: false, | ||
dashes: false | ||
}, | ||
remarkPlugins: [remarkGfm], | ||
rehypePlugins: [ | ||
rehypeComponentPreToPre, | ||
[rehypePrettyCode, prettyCodeOptions], | ||
rehypeHandleMetadata, | ||
rehypeRenderCode, | ||
rehypePreToComponentPre | ||
] | ||
}; | ||
|
||
function rehypeComponentPreToPre() { | ||
return async (tree) => { | ||
visit(tree, (node) => { | ||
if (node?.type === "element" && node?.tagName === "Components.pre") { | ||
node.tagName = "pre"; | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
function rehypePreToComponentPre() { | ||
return async (tree) => { | ||
visit(tree, (node) => { | ||
if (node?.type === "element" && node?.tagName === "pre") { | ||
node.tagName = "Components.pre"; | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
function rehypeHandleMetadata() { | ||
return async (tree) => { | ||
visit(tree, (node) => { | ||
if (node?.type === "element" && node?.tagName === "div") { | ||
if (!("data-rehype-pretty-code-fragment" in node.properties)) { | ||
return; | ||
} | ||
|
||
const preElement = node.children.at(-1); | ||
if (preElement.tagName !== "pre") { | ||
return; | ||
} | ||
|
||
if (node.children.at(0).tagName === "div") { | ||
node.properties["data-metadata"] = ""; | ||
} | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
function rehypeRenderCode() { | ||
return async (tree) => { | ||
let counter = 0; | ||
visit(tree, (node) => { | ||
if ( | ||
node?.type === "element" && | ||
(node?.tagName === "Components.pre" || node?.tagName === "pre") | ||
) { | ||
counter++; | ||
|
||
const isNonPP = counter % 2 === 0; | ||
if (isNonPP) { | ||
node.properties = { | ||
...node.properties, | ||
"data-non-pp": "" | ||
}; | ||
} | ||
|
||
/** @type HTMLElement */ | ||
const codeEl = node.children[0]; | ||
if (codeEl.tagName !== "code") { | ||
return; | ||
} | ||
|
||
const meltString = tabsToSpaces( | ||
toHtml(codeEl, { | ||
allowDangerousCharacters: true, | ||
allowDangerousHtml: true | ||
}) | ||
); | ||
|
||
codeEl.type = "raw"; | ||
codeEl.value = `{@html \`${escapeSvelte(meltString)}\`}`; | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
function tabsToSpaces(code) { | ||
return code.replaceAll(" ", " ").replaceAll("\t", " "); | ||
} |
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.