-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(markdown): create markdown package
- Loading branch information
Showing
32 changed files
with
1,088 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
"next", | ||
"solid-start", | ||
"mdx", | ||
"markdown", | ||
"shared" | ||
] | ||
] | ||
|
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 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "@content-collections/markdown", | ||
"description": "Compile markdown to html as part of your content-collection transform function", | ||
"version": "0.1.0", | ||
"type": "module", | ||
"main": "dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"import": "./dist/index.js", | ||
"types": "./dist/index.d.ts" | ||
} | ||
}, | ||
"scripts": { | ||
"build": "tsup src/index.ts --format esm --dts -d dist", | ||
"test": "vitest --run --coverage", | ||
"typecheck": "tsc" | ||
}, | ||
"dependencies": { | ||
"rehype-raw": "^7.0.0", | ||
"rehype-stringify": "^10.0.0", | ||
"remark-parse": "^11.0.0", | ||
"remark-rehype": "^11.1.0", | ||
"unified": "^11.0.4" | ||
}, | ||
"peerDependencies": { | ||
"@content-collections/core": "0.x", | ||
"react": "^18.0.0", | ||
"react-dom": "^18.2.0" | ||
}, | ||
"devDependencies": { | ||
"@content-collections/core": "workspace:*", | ||
"tsup": "^7.2.0", | ||
"typescript": "^5.3.2", | ||
"vitest": "^1.1.0" | ||
} | ||
} |
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,115 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { compileMarkdown } from "."; | ||
import { Context, Meta } from "@content-collections/core"; | ||
|
||
type Cache = Context["cache"]; | ||
|
||
const cache: Cache = (input, fn) => { | ||
return fn(input) as any; | ||
}; | ||
|
||
const sampleMeta: Meta = { | ||
directory: "post", | ||
extension: ".mdx", | ||
filePath: "post/index.mdx", | ||
fileName: "index", | ||
path: "/post", | ||
}; | ||
|
||
describe("markdown", () => { | ||
it("should convert markdown to html", async () => { | ||
const html = await compileMarkdown( | ||
{ cache }, | ||
{ | ||
_meta: sampleMeta, | ||
content: "# Hello World!", | ||
} | ||
); | ||
|
||
expect(html).toBe("<h1>Hello World!</h1>"); | ||
}); | ||
|
||
it("should apply remark plugins", async () => { | ||
const html = await compileMarkdown( | ||
{ cache }, | ||
{ | ||
_meta: sampleMeta, | ||
content: "# Hello World!", | ||
}, | ||
{ | ||
remarkPlugins: [ | ||
() => (tree) => { | ||
tree.children[0].children[0].value = "Hello Universe!"; | ||
}, | ||
], | ||
} | ||
); | ||
|
||
expect(html).toBe("<h1>Hello Universe!</h1>"); | ||
}); | ||
|
||
it("should apply rehype plugins", async () => { | ||
const html = await compileMarkdown( | ||
{ cache }, | ||
{ | ||
_meta: sampleMeta, | ||
content: "# Hello World!", | ||
}, | ||
{ | ||
rehypePlugins: [ | ||
() => (tree) => { | ||
tree.children[0].children[0].value = "Hello Universe!"; | ||
}, | ||
], | ||
} | ||
); | ||
|
||
expect(html).toBe("<h1>Hello Universe!</h1>"); | ||
}); | ||
|
||
it("should add meta to vfile data", async () => { | ||
const html = await compileMarkdown( | ||
{ cache }, | ||
{ | ||
_meta: sampleMeta, | ||
content: "# Hello World!", | ||
}, | ||
{ | ||
remarkPlugins: [ | ||
() => (tree, vfile) => { | ||
// @ts-ignore - vfile data is not typed | ||
tree.children[0].children[0].value = `Hello from ${vfile.data._meta.path}`; | ||
}, | ||
], | ||
} | ||
); | ||
|
||
expect(html).toBe("<h1>Hello from /post</h1>"); | ||
}); | ||
|
||
it("should not allow html in markdown", async () => { | ||
const html = await compileMarkdown( | ||
{ cache }, | ||
{ | ||
_meta: sampleMeta, | ||
content: "# Hello <strong>World</strong>!", | ||
} | ||
); | ||
|
||
expect(html).toBe("<h1>Hello World!</h1>"); | ||
}); | ||
|
||
it("should allow html in markdown", async () => { | ||
const html = await compileMarkdown( | ||
{ cache }, | ||
{ | ||
_meta: sampleMeta, | ||
content: "# Hello <strong>World</strong>!", | ||
},{ | ||
allowDangerousHtml: true | ||
} | ||
); | ||
|
||
expect(html).toBe("<h1>Hello <strong>World</strong>!</h1>"); | ||
}); | ||
}); |
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,53 @@ | ||
import { Context, Meta } from "@content-collections/core"; | ||
import rehypeStringify from "rehype-stringify"; | ||
import remarkParse from "remark-parse"; | ||
import remarkRehype from "remark-rehype"; | ||
import rehypeRaw from "rehype-raw"; | ||
import { Pluggable, Transformer, unified } from "unified"; | ||
|
||
type Document = { | ||
_meta: Meta; | ||
content: string; | ||
}; | ||
|
||
type Options = { | ||
allowDangerousHtml?: boolean; | ||
remarkPlugins?: Pluggable[]; | ||
rehypePlugins?: Pluggable[]; | ||
}; | ||
|
||
function addMetaToVFile(_meta: Meta) { | ||
return (): Transformer => (_, vFile) => { | ||
Object.assign(vFile.data, { _meta }); | ||
}; | ||
} | ||
|
||
async function compile(document: Document, options?: Options) { | ||
const builder = unified().use(remarkParse); | ||
builder.use(addMetaToVFile(document._meta)); | ||
|
||
if (options?.remarkPlugins) { | ||
builder.use(options.remarkPlugins); | ||
} | ||
|
||
builder.use(remarkRehype, { allowDangerousHtml: options?.allowDangerousHtml }); | ||
if (options?.allowDangerousHtml) { | ||
builder.use(rehypeRaw); | ||
} | ||
|
||
if (options?.rehypePlugins) { | ||
builder.use(options.rehypePlugins); | ||
} | ||
|
||
const html = await builder.use(rehypeStringify).process(document.content); | ||
|
||
return String(html); | ||
} | ||
|
||
export function compileMarkdown( | ||
{ cache }: Pick<Context, "cache">, | ||
document: Document, | ||
options?: Options | ||
) { | ||
return cache(document, (doc) => compile(doc, options)); | ||
} |
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,22 @@ | ||
{ | ||
"compilerOptions": { | ||
/* Base Options: */ | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"target": "es2022", | ||
"allowJs": true, | ||
"resolveJsonModule": true, | ||
"moduleDetection": "force", | ||
"isolatedModules": true, | ||
/* Strictness */ | ||
"strict": true, | ||
"noUncheckedIndexedAccess": true, | ||
/* If NOT transpiling with TypeScript: */ | ||
"moduleResolution": "Bundler", | ||
"module": "ESNext", | ||
"noEmit": true, | ||
/* If your code doesn't run in the DOM: */ | ||
"lib": ["es2022"], | ||
}, | ||
"include": ["src/**/*"] | ||
} |
Oops, something went wrong.