Skip to content

Commit a5cdd00

Browse files
committed
chore: Replace AppState singleton to an instance.
1 parent 60cf917 commit a5cdd00

7 files changed

Lines changed: 126 additions & 139 deletions

File tree

main.js

Lines changed: 101 additions & 88 deletions
Large diffs are not rendered by default.

src/abstract-wp-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export abstract class AbstractWordPressClient implements WordPressClient {
135135
auth,
136136
postParams
137137
});
138-
const html = AppState.getInstance().markdownParser.render(postParams.content);
138+
const html = AppState.markdownParser.render(postParams.content);
139139
const result = await this.publish(
140140
postParams.title ?? 'A post from Obsidian!',
141141
html,

src/app-state.ts

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,27 @@
11
import { Events } from 'obsidian';
22
import MarkdownIt from 'markdown-it';
33
import { MarkdownItImagePluginInstance } from './markdown-it-image-plugin';
4-
import { isEmpty, trim } from 'lodash-es';
4+
import { MarkdownItPlugin } from './types';
55

6-
7-
export class AppState {
8-
private static instance: AppState;
6+
class AppStore {
97

108
markdownParser = new MarkdownIt()
119
.use(MarkdownItImagePluginInstance.plugin);
1210

1311
events = new Events();
1412

15-
/**
16-
* Code verifier between classes.
17-
*/
1813
codeVerifier: string | undefined;
1914

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>();
4916

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);
5519
}
5620

21+
getMarkdownItPlugin(name: string): MarkdownItPlugin | undefined {
22+
return this.#markdownPlugins.get(name);
23+
}
5724
}
25+
26+
export const AppState = new AppStore();
27+

src/main.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export default class WordpressPlugin extends Plugin {
9797
}
9898
}
9999

100-
AppState.getInstance().markdownParser.set({
100+
AppState.markdownParser.set({
101101
html: this.#settings?.enableHtml ?? false
102102
});
103103
}
@@ -152,15 +152,15 @@ export default class WordpressPlugin extends Plugin {
152152
error: e.error,
153153
desc: e.error_description.replace(/\+/g,' ')
154154
}));
155-
AppState.getInstance().events.trigger(EventType.OAUTH2_TOKEN_GOT, undefined);
155+
AppState.events.trigger(EventType.OAUTH2_TOKEN_GOT, undefined);
156156
} else if (e.code) {
157157
const token = await OAuth2Client.getWpOAuth2Client(this).getToken({
158158
code: e.code,
159159
redirectUri: WP_OAUTH2_REDIRECT_URI,
160-
codeVerifier: AppState.getInstance().codeVerifier
160+
codeVerifier: AppState.codeVerifier
161161
});
162162
console.log(token);
163-
AppState.getInstance().events.trigger(EventType.OAUTH2_TOKEN_GOT, token);
163+
AppState.events.trigger(EventType.OAUTH2_TOKEN_GOT, token);
164164
}
165165
}
166166
}

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { SafeAny } from './utils';
22
import { isArray, isString } from 'lodash-es';
33

4+
export interface MarkdownItPlugin {
5+
updateOptions: <OptionType> (option: OptionType) => void;
6+
}
7+
48
export type MatterData = { [p: string]: SafeAny };
59

610
export interface Media {

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function isPromiseFulfilledResult<T>(obj: SafeAny): obj is PromiseFulfill
3030
}
3131

3232
export function setupMarkdownParser(settings: WordpressPluginSettings): void {
33-
AppState.getInstance().markdownParser.use(MarkdownItMathJax3Plugin, {
33+
AppState.markdownParser.use(MarkdownItMathJax3Plugin, {
3434
outputType: settings.mathJaxOutputType
3535
});
3636
}

src/wp-profile-modal.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class WpProfileModal extends AbstractModal {
6262
super(plugin);
6363

6464
this.profileData = Object.assign({}, profile);
65-
this.tokenGotRef = AppState.getInstance().events.on(EventType.OAUTH2_TOKEN_GOT, async token => {
65+
this.tokenGotRef = AppState.events.on(EventType.OAUTH2_TOKEN_GOT, async token => {
6666
this.profileData.wpComOAuth2Token = token;
6767
if (atIndex >= 0) {
6868
// if token is undefined, just remove it
@@ -270,19 +270,19 @@ class WpProfileModal extends AbstractModal {
270270

271271
onClose() {
272272
if (this.tokenGotRef) {
273-
AppState.getInstance().events.offref(this.tokenGotRef);
273+
AppState.events.offref(this.tokenGotRef);
274274
}
275275
const { contentEl } = this;
276276
contentEl.empty();
277277
}
278278

279279
private async refreshWpComToken(): Promise<void> {
280-
AppState.getInstance().codeVerifier = generateCodeVerifier();
280+
AppState.codeVerifier = generateCodeVerifier();
281281
await OAuth2Client.getWpOAuth2Client(this.plugin).getAuthorizeCode({
282282
redirectUri: WP_OAUTH2_REDIRECT_URI,
283283
scope: [ 'posts', 'taxonomy', 'media', 'sites' ],
284284
blog: this.profileData.endpoint,
285-
codeVerifier: AppState.getInstance().codeVerifier
285+
codeVerifier: AppState.codeVerifier
286286
});
287287
}
288288

0 commit comments

Comments
 (0)