Skip to content

Commit

Permalink
feat: 添加rehype-raw支持,更新Markdown解析器以处理HTML标签
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuai2144 committed Feb 20, 2025
1 parent e232e3e commit c726b21
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"react-resizable-panels": "^2.1.7",
"react-use": "^17.6.0",
"rehype-katex": "^7.0.1",
"rehype-raw": "^7.0.0",
"remark": "^15.0.1",
"remark-frontmatter": "^5.0.0",
"remark-gfm": "^4.0.1",
Expand Down
49 changes: 49 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/MarkdownEditor/editor/elements/media.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ export function Media({
},
}}
trigger="click"
open={state().selected ? undefined : false}
open={state().selected && !readonly ? undefined : false}
content={
<ActionIconBox
title="删除"
Expand Down
19 changes: 17 additions & 2 deletions src/MarkdownEditor/editor/parser/parserMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ const parserBlock = (
.replace('-->', '')
.trim() || '{}';

if (value) {
if (value && currentElement?.value.trim()?.startsWith('<!--')) {
try {
contextProps = json5.parse(value);
} catch (e) {
Expand Down Expand Up @@ -338,7 +338,7 @@ const parserBlock = (
el = { type: 'break', children: [{ text: '' }] };
} else {
const htmlMatch = currentElement.value.match(
/<\/?(b|i|del|code|span|a)[^\n>]*?>/,
/<\/?(b|i|del|font|code|span|sup|sub|a)[^\n>]*?>/,
);
if (htmlMatch) {
const [str, tag] = htmlMatch;
Expand Down Expand Up @@ -380,6 +380,14 @@ const parserBlock = (
url: url[1],
});
}
} else if (tag === 'font') {
const color = str.match(/color="([^"\n]+)"/);
if (color) {
htmlTag.push({
tag: tag,
color: color[1],
});
}
} else {
htmlTag.push({ tag: tag });
}
Expand Down Expand Up @@ -698,6 +706,13 @@ const parserBlock = (
el = { text: currentElement.value };
if (currentElement.value) {
for (let t of htmlTag) {
if (t.tag === 'font') {
console.log(t);
el.color = t.color;
}
if (t.tag === 'sup') el.identifier = el.text;
if (t.tag === 'sub') el.identifier = el.text;

if (t.tag === 'code') el.code = true;
if (t.tag === 'i') el.italic = true;
if (t.tag === 'b' || t.tag === 'strong') el.bold = true;
Expand Down
4 changes: 3 additions & 1 deletion src/MarkdownEditor/editor/parser/remarkParse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import rehypeKatex from 'rehype-katex';
import rehypeRaw from 'rehype-raw';
import remarkFrontmatter from 'remark-frontmatter';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';
Expand All @@ -9,7 +10,8 @@ import { unified } from 'unified';
const parser = unified()
.use(remarkParse)
.use(remarkMath as any)
.use(remarkRehype as any)
.use(remarkRehype as any, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeKatex as any)
.use(remarkGfm)
.use(remarkFrontmatter, ['yaml']);
Expand Down

1 comment on commit c726b21

@vercel
Copy link

@vercel vercel bot commented on c726b21 Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.