-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix markdown converter (Promise/await issues)
- Loading branch information
Showing
6 changed files
with
96 additions
and
68 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 |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import { marked } from "marked"; | ||
import { | ||
createWalkTokensExtension, | ||
rendererExtension, | ||
} from "@fumix/fu-blog-common"; | ||
|
||
marked.use( | ||
createWalkTokensExtension((url) => fetch(url).then((it) => it.text())), | ||
); | ||
marked.use(rendererExtension); | ||
import { MarkdownConverter } from "@fumix/fu-blog-common"; | ||
import DOMPurify from "dompurify"; | ||
|
||
export class MarkdownConverterClient extends MarkdownConverter { | ||
private static instance: MarkdownConverterClient; | ||
|
||
protected override dompurify: DOMPurify.DOMPurifyI = DOMPurify; | ||
|
||
private constructor() { | ||
super((url: string) => fetch(url).then((it) => it.text())); | ||
} | ||
|
||
public static get Instance() { | ||
return this.instance ?? (this.instance = new this()); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,22 @@ | ||
import { marked } from "marked"; | ||
import { createWalkTokensExtension, rendererExtension } from "@fumix/fu-blog-common"; | ||
import { MarkdownConverter } from "@fumix/fu-blog-common"; | ||
import fetch from "node-fetch"; | ||
import DOMPurify from "dompurify"; | ||
import { JSDOM } from "jsdom"; | ||
|
||
marked.use(createWalkTokensExtension((url) => fetch(url).then((it) => it.text()))); | ||
marked.use(rendererExtension); | ||
export class MarkdownConverterServer extends MarkdownConverter { | ||
private static instance: MarkdownConverterServer; | ||
|
||
/** | ||
* On the server side, we need to pass a new window object to DOMPurify. | ||
* It doesn't work to just call `DOMPurify.sanitize()` directly like on the client side. | ||
*/ | ||
export const createDomPurify: () => DOMPurify.DOMPurifyI = // | ||
() => DOMPurify(new JSDOM("").window as unknown as Window); | ||
/** | ||
* On the server side, we need to pass a new window object to DOMPurify. | ||
* It doesn't work to just call `DOMPurify.sanitize()` directly like on the client side. | ||
*/ | ||
protected override dompurify: DOMPurify.DOMPurifyI = DOMPurify(new JSDOM("").window as unknown as Window); | ||
|
||
private constructor() { | ||
super((url: string) => fetch(url).then((it) => it.text())); | ||
} | ||
|
||
public static get Instance() { | ||
return this.instance ?? (this.instance = new this()); | ||
} | ||
} |
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