Skip to content

Commit

Permalink
fix: customized markdown-it extensions have lower priority(#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
imzbf committed Jun 25, 2023
1 parent 9d5be64 commit 69f2234
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
22 changes: 22 additions & 0 deletions dev/Preview/image/TargetBlankExtension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const TargetBlankExtension = (md) => {
const defaultRender =
md.renderer.rules.link_open ||
function (tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options);
};

md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
const aIndex = tokens[idx].attrIndex('target');

if (aIndex < 0) {
tokens[idx].attrPush(['target', '_blank']);
} else {
tokens[idx].attrs[aIndex][1] = '_blank';
}

// pass token to default renderer.
return defaultRender(tokens, idx, options, env, self);
};
};

export default TargetBlankExtension;
9 changes: 6 additions & 3 deletions dev/Preview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type { ExposeParam } from '~~/index';
import mdText from '../data.md';
import { Theme } from '../App';
import axios from 'axios';
// import TargetBlankExtension from './image/TargetBlankExtension.js';
// import 'katex/dist/katex.min.css';

// import { Extension } from '@codemirror/state';
Expand Down Expand Up @@ -70,9 +71,11 @@ config({
return [...extensions, lineNumbers()];
},
// markdownItConfig: (mdit) => {
// mdit.use(ancher, {
// permalink: true
// });
// mdit.use(ancher, {
// permalink: true
// });

// mdit.use(TargetBlankExtension);
// },
editorExtensions: {
// prettier: {
Expand Down
46 changes: 24 additions & 22 deletions packages/MdEditor/layouts/Content/composition/useMarkdownIt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,34 @@ const initLineNumber = (md: mdit) => {
'ordered_list_open',
'bullet_list_open',
'blockquote_open',
'hr'
'hr',
'html_block',
'fence'
].forEach((rule) => {
md.renderer.rules[rule] = (tokens, idx, options, _env, self) => {
let line;
if (tokens[idx].map && tokens[idx].level === 0) {
line = tokens[idx].map![0];
tokens[idx].attrSet('data-line', String(line));
}
return self.renderToken(tokens, idx, options);
};
});

['html_block', 'fence'].forEach((rule) => {
const backup = md.renderer.rules[rule];

md.renderer.rules[rule] = (tokens, idx, options, env, self) => {
let line;
const _htmlCode = backup!(tokens, idx, options, env, self);

if (tokens[idx].map && tokens[idx].level === 0) {
line = tokens[idx].map![0];
return _htmlCode.replace(/^(<[^>]*)/, `$1 data-line="${line}"`);
}
if (!backup) {
md.renderer.rules[rule] = (tokens, idx, options, _env, self) => {
let line;
if (tokens[idx].map && tokens[idx].level === 0) {
line = tokens[idx].map![0];
tokens[idx].attrSet('data-line', String(line));
}
return self.renderToken(tokens, idx, options);
};
} else {
md.renderer.rules[rule] = (tokens, idx, options, env, self) => {
let line;
const _htmlCode = backup(tokens, idx, options, env, self);

if (tokens[idx].map && tokens[idx].level === 0) {
line = tokens[idx].map![0];
return _htmlCode.replace(/^(<[^>]*)/, `$1 data-line="${line}"`);
}

return _htmlCode;
};
return _htmlCode;
};
}
});
};

Expand Down
8 changes: 0 additions & 8 deletions packages/MdEditor/layouts/Content/markdownIt/heading/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ export interface HeadingPluginOps extends markdownit.Options {
}

const HeadingPlugin = (md: markdownit, options: HeadingPluginOps) => {
let Token: any;

md.core.ruler.push('headingLinks', (state) => {
if (!Token) {
Token = state.Token;
}
});

md.renderer.rules.heading_open = (tokens, idx) => {
const token = tokens[idx];

Expand Down

0 comments on commit 69f2234

Please sign in to comment.