Skip to content

Commit 08073c2

Browse files
committed
chore: Update code structure of MarkdownIt plugins.
1 parent a5cdd00 commit 08073c2

3 files changed

Lines changed: 37 additions & 27 deletions

File tree

src/app-state.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
import { Events } from 'obsidian';
22
import MarkdownIt from 'markdown-it';
33
import { MarkdownItImagePluginInstance } from './markdown-it-image-plugin';
4-
import { MarkdownItPlugin } from './types';
4+
import { MarkdownItCommentPluginInstance } from './markdown-it-comment-plugin';
5+
import { MarkdownItMathJax3PluginInstance } from './markdown-it-mathjax3-plugin';
56

67
class AppStore {
78

8-
markdownParser = new MarkdownIt()
9-
.use(MarkdownItImagePluginInstance.plugin);
9+
markdownParser = new MarkdownIt();
1010

1111
events = new Events();
1212

1313
codeVerifier: string | undefined;
1414

15-
#markdownPlugins = new Map<string, MarkdownItPlugin>();
16-
17-
registerMarkdownItPlugin(name: string, plugin: MarkdownItPlugin): void {
18-
this.#markdownPlugins.set(name, plugin);
19-
}
20-
21-
getMarkdownItPlugin(name: string): MarkdownItPlugin | undefined {
22-
return this.#markdownPlugins.get(name);
23-
}
2415
}
2516

2617
export const AppState = new AppStore();
2718

19+
AppState.markdownParser
20+
.use(MarkdownItCommentPluginInstance.plugin)
21+
.use(MarkdownItMathJax3PluginInstance.plugin)
22+
.use(MarkdownItImagePluginInstance.plugin);

src/markdown-it-image-plugin.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import Token from 'markdown-it/lib/token';
33
import { trim } from 'lodash-es';
44

55

6+
const tokenType = 'ob_img';
7+
68
export interface MarkdownItImageActionParams {
79
src: string;
810
width?: string;
@@ -18,21 +20,21 @@ const pluginOptions: MarkdownItImagePluginOptions = {
1820
}
1921

2022
export const MarkdownItImagePluginInstance = {
21-
plugin: pluginImpl,
23+
plugin: plugin,
2224
doWithImage: (action: (img: MarkdownItImageActionParams) => void) => {
2325
pluginOptions.doWithImage = action;
2426
},
2527
}
2628

27-
function pluginImpl(md: MarkdownIt): void {
28-
md.inline.ruler.after('image', 'ob_img', (state, silent) => {
29+
function plugin(md: MarkdownIt): void {
30+
md.inline.ruler.after('image', tokenType, (state, silent) => {
2931
const regex = /^!\[\[([^|\]\n]+)(\|([^\]\n]+))?\]\]/;
3032
const match = state.src.slice(state.pos).match(regex);
3133
if (match) {
3234
if (silent) {
3335
return true;
3436
}
35-
const token = state.push('ob_img', 'img', 0);
37+
const token = state.push(tokenType, 'img', 0);
3638
const matched = match[0];
3739
const src = match[1];
3840
const size = match[3];

src/markdown-it-mathjax3-plugin.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,48 @@ import juice from 'juice';
1313
import { SafeAny } from './utils';
1414
import { MathJaxOutputType } from './plugin-settings';
1515

16+
const inlineTokenType = 'math_inline';
17+
const blockTokenType = 'math_block';
1618

1719
interface MarkdownItMathJax3PluginOptions {
1820
outputType: MathJaxOutputType;
1921
}
2022

23+
const pluginOptions: MarkdownItMathJax3PluginOptions = {
24+
outputType: MathJaxOutputType.TeX,
25+
}
26+
2127
interface ConvertOptions {
2228
display: boolean
2329
}
2430

25-
export default function MarkdownItMathJax3Plugin(md: MarkdownIt, options: MarkdownItMathJax3PluginOptions): void {
31+
export const MarkdownItMathJax3PluginInstance = {
32+
plugin: plugin,
33+
updateOutputType: (type: MathJaxOutputType) => {
34+
pluginOptions.outputType = type;
35+
},
36+
}
37+
38+
function plugin(md: MarkdownIt): void {
2639
// set MathJax as the renderer for markdown-it-simplemath
27-
md.inline.ruler.after('escape', 'math_inline', mathInline);
28-
md.block.ruler.after('blockquote', 'math_block', mathBlock, {
40+
md.inline.ruler.after('escape', inlineTokenType, mathInline);
41+
md.block.ruler.after('blockquote', blockTokenType, mathBlock, {
2942
alt: ['paragraph', 'reference', 'blockquote', 'list'],
3043
});
31-
md.renderer.rules.math_inline = (tokens: Token[], idx: number) => {
44+
md.renderer.rules[inlineTokenType] = (tokens: Token[], idx: number) => {
3245
return renderMath(tokens[idx].content, {
3346
display: false
34-
}, options);
47+
});
3548
};
36-
md.renderer.rules.math_block = (tokens: Token[], idx: number) => {
49+
md.renderer.rules[blockTokenType] = (tokens: Token[], idx: number) => {
3750
return renderMath(tokens[idx].content, {
3851
display: true
39-
}, options);
52+
});
4053
};
4154
}
4255

43-
function renderMath(content: string, convertOptions: ConvertOptions, options: MarkdownItMathJax3PluginOptions): string {
44-
if (options.outputType === MathJaxOutputType.SVG) {
56+
function renderMath(content: string, convertOptions: ConvertOptions): string {
57+
if (pluginOptions.outputType === MathJaxOutputType.SVG) {
4558
const documentOptions = {
4659
InputJax: new TeX({ packages: AllPackages }),
4760
OutputJax: new SVG({ fontCache: 'none' })
@@ -154,7 +167,7 @@ function mathInline(state: StateInline, silent: boolean) {
154167
}
155168

156169
if (!silent) {
157-
const token = state.push('math_inline', 'math', 0);
170+
const token = state.push(inlineTokenType, 'math', 0);
158171
token.markup = '$';
159172
token.content = state.src.slice(start, match);
160173
}
@@ -214,7 +227,7 @@ function mathBlock(state: StateBlock, start: number, end: number, silent: boolea
214227

215228
state.line = next + 1;
216229

217-
const token = state.push('math_block', 'math', 0);
230+
const token = state.push(blockTokenType, 'math', 0);
218231
token.block = true;
219232
token.content =
220233
(firstLine && firstLine.trim() ? firstLine + '\n' : '') +

0 commit comments

Comments
 (0)