-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Timothée Rebours
committed
Feb 22, 2024
1 parent
6a63cf9
commit 9f05a2f
Showing
5 changed files
with
73 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,62 @@ | ||
import type { Node, Root, Text, Parent, RootContent } from 'mdast' | ||
import { unified } from 'unified' | ||
import remarkParse from 'remark-parse' | ||
import remarkMDX from 'remark-mdx' | ||
import remarkHtml from 'remark-html' | ||
|
||
function assertsType<T> (val: unknown): asserts val is T {} | ||
|
||
const truncateMDXContent = (limit: number): ((x: Root) => void) => { | ||
if (limit === -1) return () => {} | ||
let charactersCount = 0 | ||
let reachedLimit = false | ||
|
||
const truncateTree = (node: Node | Root): Node | null => { | ||
if (reachedLimit) { | ||
return null | ||
} | ||
|
||
if (node.type === 'text') { | ||
assertsType<Text>(node) | ||
const remainingCharacters = limit - charactersCount | ||
if (node.value.length > remainingCharacters) { | ||
node.value = node.value.substring(0, remainingCharacters) + '...' | ||
reachedLimit = true | ||
} | ||
charactersCount += node.value.length | ||
} else if ('children' in node) { | ||
assertsType<Parent>(node) | ||
node.children = node.children.map(truncateTree).filter(x => x != null) as RootContent[] | ||
} | ||
|
||
return node | ||
} | ||
|
||
return (tree: Root) => { | ||
truncateTree(tree) | ||
} | ||
} | ||
|
||
const removeImportsAndExports = () => { | ||
return (tree: Root) => { | ||
tree.children = tree.children.filter((node: Node) => { | ||
return node.type !== 'mdxjsEsm' && // import tags | ||
node.type !== 'mdxJsxFlowElement' // custom components (Image, Pintora) | ||
}) | ||
} | ||
} | ||
export const convertMDXToHTML = async (mdxContent: string, maxChars?: number): Promise<string> => { | ||
try { | ||
const file = await unified() | ||
.use(remarkParse) // Parse the Markdown syntax | ||
.use(remarkMDX) // Parse the MDX syntax | ||
.use(removeImportsAndExports) | ||
.use(truncateMDXContent, maxChars ?? -1) | ||
.use(remarkHtml) // Convert to HTML | ||
.process(mdxContent) // Process the MDX content | ||
return String(file) | ||
} catch (error) { | ||
console.error('Error converting MDX to HTML:', error) | ||
throw error | ||
} | ||
} |