Skip to content

Commit

Permalink
initial files
Browse files Browse the repository at this point in the history
  • Loading branch information
kwshi committed Feb 2, 2021
0 parents commit 2ca07b0
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
40 changes: 40 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "rehype-katex-svelte",
"version": "1.0.0",
"description": "Like rehype-katex, but Svelte-friendly",
"main": "dist/index.js",
"types": "index.d.ts",
"scripts": {
"prepublish": "tsc"
},
"repository": {
"type": "git",
"url": "git+https://github.com/kwshi/rehype-katex-svelte.git"
},
"keywords": [
"rehype",
"katex",
"svelte",
"mdsvex",
"svx"
],
"author": "Kye Shi",
"license": "MIT",
"bugs": {
"url": "https://github.com/kwshi/rehype-katex-svelte/issues"
},
"homepage": "https://github.com/kwshi/rehype-katex-svelte#readme",
"dependencies": {
"hast-util-from-string": "^1.0.4",
"hast-util-select": "^4.0.2",
"hast-util-to-string": "^1.0.4",
"katex": "^0.12.0",
"vfile": "^4.2.1"
},
"devDependencies": {
"@types/hast": "^2.3.1",
"@types/katex": "^0.11.0",
"@types/unist": "^2.0.3",
"typescript": "^4.1.3"
}
}
191 changes: 191 additions & 0 deletions pnpm-lock.yaml

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

4 changes: 4 additions & 0 deletions src/hast-util-from-string.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "hast-util-from-string" {
import type { Node } from "hast";
export default function (node: Node, value: string): void;
}
4 changes: 4 additions & 0 deletions src/hast-util-select.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "hast-util-select" {
import type { Node, Element } from "hast";
export const selectAll: (selector: string, node: Node) => Element[];
}
4 changes: 4 additions & 0 deletions src/hast-util-to-string.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "hast-util-to-string" {
import type { Node } from "hast";
export default function (node: Node): string;
}
22 changes: 22 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { selectAll } from "hast-util-select";
import hastToString from "hast-util-to-string";
import hastFromString from "hast-util-from-string";
import type { Node } from "hast";
import type { VFile } from "vfile";

import { renderToString } from "katex";
import type { KatexOptions } from "katex";

export default (options: KatexOptions = {}) => (tree: Node, file: VFile) => {
if (file.filename !== "src/routes/index.svx") return;
for (const node of selectAll(".math", tree)) {
const displayMode = (<string[] | undefined>(
node.properties?.className
))?.includes("math-display");
const rendered = renderToString(hastToString(node), {
...options,
displayMode,
});
hastFromString(node, `{@html ${JSON.stringify(rendered)}}`);
}
};
10 changes: 10 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["es2017", "dom"],
"module": "commonjs",
"strict": true,
"outDir": "dist",
"declaration": true
}
}

0 comments on commit 2ca07b0

Please sign in to comment.