|
1 | 1 | import { Events } from 'obsidian'; |
2 | 2 | import MarkdownIt from 'markdown-it'; |
3 | 3 | import { MarkdownItImagePluginInstance } from './markdown-it-image-plugin'; |
4 | | -import { isEmpty, trim } from 'lodash-es'; |
| 4 | +import { MarkdownItPlugin } from './types'; |
5 | 5 |
|
6 | | - |
7 | | -export class AppState { |
8 | | - private static instance: AppState; |
| 6 | +class AppStore { |
9 | 7 |
|
10 | 8 | markdownParser = new MarkdownIt() |
11 | 9 | .use(MarkdownItImagePluginInstance.plugin); |
12 | 10 |
|
13 | 11 | events = new Events(); |
14 | 12 |
|
15 | | - /** |
16 | | - * Code verifier between classes. |
17 | | - */ |
18 | 13 | codeVerifier: string | undefined; |
19 | 14 |
|
20 | | - private constructor() { |
21 | | - this.markdownParser.renderer.rules.image = (tokens, idx) => { |
22 | | - const token = tokens[idx]; |
23 | | - const srcIndex = token.attrIndex('src'); |
24 | | - const src = token.attrs ? token.attrs[srcIndex][1] : ''; |
25 | | - const altText = token.content; |
26 | | - |
27 | | - const [ alt, size] = altText.split('|'); |
28 | | - let width; |
29 | | - let height; |
30 | | - if (!isEmpty(size)) { |
31 | | - const sepIndex = size.indexOf('x'); // width x height |
32 | | - if (sepIndex > 0) { |
33 | | - width = trim(size.substring(0, sepIndex)); |
34 | | - height = trim(size.substring(sepIndex + 1)); |
35 | | - } else { |
36 | | - width = trim(size); |
37 | | - } |
38 | | - } |
39 | | - if (width) { |
40 | | - if (height) { |
41 | | - return `<img src="${src}" width="${width}" height="${height}" alt="${alt}">`; |
42 | | - } |
43 | | - return `<img src="${src}" width="${width}" alt="${alt}">`; |
44 | | - } else { |
45 | | - return `<img src="${src}" alt="${alt}">`; |
46 | | - } |
47 | | - }; |
48 | | - } |
| 15 | + #markdownPlugins = new Map<string, MarkdownItPlugin>(); |
49 | 16 |
|
50 | | - static getInstance(): AppState { |
51 | | - if (!AppState.instance) { |
52 | | - AppState.instance = new AppState(); |
53 | | - } |
54 | | - return AppState.instance; |
| 17 | + registerMarkdownItPlugin(name: string, plugin: MarkdownItPlugin): void { |
| 18 | + this.#markdownPlugins.set(name, plugin); |
55 | 19 | } |
56 | 20 |
|
| 21 | + getMarkdownItPlugin(name: string): MarkdownItPlugin | undefined { |
| 22 | + return this.#markdownPlugins.get(name); |
| 23 | + } |
57 | 24 | } |
| 25 | + |
| 26 | +export const AppState = new AppStore(); |
| 27 | + |
0 commit comments