Skip to content

Commit

Permalink
fix: 未正确展示标题中的md语法内容(#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
zbf committed Nov 23, 2021
1 parent 242aefa commit 3c85aae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 9 additions & 3 deletions MdEditor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,16 @@ Editor.defaultProps = {
showCodeRowNumber: false,
screenfullJs: screenfullUrl,
previewTheme: 'default',
markedHeading: (text, level) => {
markedHeading: (text, level, raw) => {
// 我们默认同一级别的标题,你不会定义两个相同的
const id = markedHeadingId(text, level);
return `<h${level} id="${id}"><a href="#${id}">${text}</a></h${level}>`;
const id = markedHeadingId(raw, level);

// 如果标题有markdown语法内容,会按照该语法添加标题,而不再自定义,但是仍然支持目录定位
if (text !== raw) {
return `<h${level} id="${id}">${text}</h${level}>`;
} else {
return `<h${level} id="${id}"><a href="#${id}">${raw}</a></h${level}>`;
}
},
// 希望你在自定义markedHeading的同时,能够告诉编辑器你生成ID的算法~
markedHeadingId: markedHeadingId,
Expand Down
4 changes: 2 additions & 2 deletions MdEditor/layouts/Content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ const Content = (props: EditorContentProp) => {
const renderer = new marked.Renderer();
// 标题重构
renderer.heading = (...headProps) => {
const [text, level] = headProps;
headstemp.push({ text, level });
const [, level, raw] = headProps;
headstemp.push({ text: raw, level });

return props.markedHeading(...headProps);
};
Expand Down

0 comments on commit 3c85aae

Please sign in to comment.