Skip to content

Commit

Permalink
fix: add vFileShim to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SharzyL committed Apr 11, 2024
1 parent f6de922 commit 6ef872f
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@ function getMetadata(options) {
}
}

// When `unified` runs process(value), it converts `value` to vFile class if `value` does not look like a vFile.
// However, the constructor of vFile calls proc.cwd, which is not provided in vite runtime
// To avoid vFile complaining “proc.cwd is not a function”, we use a fake vFile class that stringifies as usual but
// never calls proc.cwd.
// FUCK JAVASCRIPT
class vFileShim {
constructor(value) {
this.value = value
this.messages = []
this.message = (reason, place, origin) => {
const msg = new VFileMessage(reason, place, origin)
msg.fatal = false
this.messages.push(msg)
return msg
}
}

toString() {
return this.value
}
}

export function makeMarkdown(content) {
const metadata = { title: defaultTitle, description: "" }
const convertedHtml = unified()
Expand All @@ -41,7 +63,7 @@ export function makeMarkdown(content) {
.use(getMetadata, { result: metadata }) // result is written to `metadata` variable
.use(remarkRehype)
.use(rehypeStringify)
.processSync(content)
.processSync(new vFileShim(content))

return `<!DOCTYPE html>
<html lang='en'>
Expand Down

0 comments on commit 6ef872f

Please sign in to comment.