Skip to content

Commit f8e09da

Browse files
committed
refactor: replace Markdoc parsing with custom tokenizer
- Updated the Markdoc parsing implementation in both `markdoc-html-string.ts` and `markdoc-parser.tsx` to utilize the new `parseMarkdocDocument` function from `markdoc-tokenizer`. - This change aims to improve the parsing process and maintain consistency across the markdown handling components.
1 parent 6069ad9 commit f8e09da

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

src/lib/markdown/markdoc-html-string.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Markdoc, { type Node, Tag } from "@markdoc/markdoc";
22
import { removeMarkdownSyntax } from "@/lib/utils";
3+
import { parseMarkdocDocument } from "./markdoc-tokenizer";
34

45
function safeForCdata(html: string): string {
56
return html.replace(/]]>/g, "]]]]><![CDATA[>");
@@ -43,7 +44,7 @@ export function markdocToHtmlForRss(
4344
const prefix = featureUrl ? featureImageMarkup(featureUrl, alt) : "";
4445

4546
try {
46-
const ast = Markdoc.parse(md);
47+
const ast = parseMarkdocDocument(md);
4748
const content = Markdoc.transform(ast, {
4849
tags: {
4950
youtube: {

src/lib/markdown/markdoc-parser.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import Markdoc from "@markdoc/markdoc";
22
import React from "react";
3+
import { parseMarkdocDocument } from "./markdoc-tokenizer";
34
import { YoutubeTag, YoutubeTagConfig } from "./markdown-tags/youtube";
45
import { CodeTag, CodeTagConfig } from "./markdown-tags/code";
56
import { LivecodeTag, LivecodeTagConfig } from "./markdown-tags/livecode";
67

78
export const markdocParser = (markdown: string) => {
8-
const ast = Markdoc.parse(markdown);
9+
const ast = parseMarkdocDocument(markdown);
910
const content = Markdoc.transform(ast, {
1011
tags: {
1112
youtube: YoutubeTagConfig,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Markdoc from "@markdoc/markdoc";
2+
3+
/** Shared tokenizer: bare URLs (e.g. `https://…`) become real links, not plain text. */
4+
const tokenizer = new Markdoc.Tokenizer({ linkify: true });
5+
6+
export function parseMarkdocDocument(markdown: string) {
7+
return Markdoc.parse(tokenizer.tokenize(markdown));
8+
}

0 commit comments

Comments
 (0)