How to use Asynchronous Plugins in Remark? #1314
Answered
by
ChristianMurphy
fwx5618177
asked this question in
Q&A
-
Here are the links: Here are sample: index.ts import { Providers, TextNode, Options } from 'remark-excalidraw';
import { visit } from 'unist-util-visit';
import { RuntimeFactory } from './http/RuntimeFactory';
import { COMMON } from './constants';
import { HelperFactory } from './helpers/HelperFactory';
const remarkExcalidraw = (options?: Options) => {
return async (tree: TextNode) => {
RuntimeFactory.getInstance();
const providers = await RuntimeFactory.getHttpStrategy().requestProvider<Providers>(
COMMON.OEMBED_PROVIDERS_URL,
);
const ctx = {
...options,
providers: [...providers, ...COMMON.DEFAULT_DRAW_PROVIDER],
};
visit(tree, 'text', (node: TextNode) => {
Promise.all([HelperFactory.getInstance().process(ctx, node)]);
});
};
};
export default remarkExcalidraw; |
Beta Was this translation helpful? Give feedback.
Answered by
ChristianMurphy
Apr 21, 2024
Replies: 2 comments 8 replies
-
Plugins can be asynchronous, but import { Providers, TextNode, Options } from 'remark-excalidraw';
import { visit } from 'unist-util-visit';
import { RuntimeFactory } from './http/RuntimeFactory';
import { COMMON } from './constants';
import { HelperFactory } from './helpers/HelperFactory';
const remarkExcalidraw = (options?: Options) => {
return async (tree: TextNode) => {
RuntimeFactory.getInstance();
const providers = await RuntimeFactory.getHttpStrategy().requestProvider<Providers>(
COMMON.OEMBED_PROVIDERS_URL,
);
const ctx = {
...options,
providers: [...providers, ...COMMON.DEFAULT_DRAW_PROVIDER],
};
let promises: PromiseLike[] = []
visit(tree, 'text', (node: TextNode) => {
promises.push(HelperFactory.getInstance().process(ctx, node));
});
await Promise.all(promises)
};
};
export default remarkExcalidraw; |
Beta Was this translation helpful? Give feedback.
2 replies
-
@ChristianMurphy @remcohaszing Besides that, I have some other confusions:
Here is the plugin's handling module: import { ContentStrategy } from 'remark-excalidraw';
import { BaseContentStrategy } from './BaseContentStrategy';
export class ExcalidrawStrategy extends BaseContentStrategy implements ContentStrategy {
override handleContent(data: any): string {
if (!this.node) {
this.logInfo('No node found');
throw new Error('No node found');
}
this.node.data = {
extra: {
type: this.node.data?.extra?.type || 'html',
sorts: 'excalidraw',
value: data.toString(),
},
};
// TODO: it's test case
// this.node = {
// type: 'html',
// value: data.toString(),
// };
return data;
}
} Here is the test file: it('should render markdown with asynchronous processing', function () {
this.timeout(10000);
const ast = remark().use(remarkExcalidraw).parse(markdownContent);
visit(ast, 'text', (node: any) => {
console.log(node);
});
}); |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Then use
unified.process
orunified.run
with async await https://github.com/unifiedjs/unified?tab=readme-ov-file#overviewI understand, but have you passed a reference to the node or a copy of the node?
It is a concern, misuse of inheritance has been known to be an anti-pattern for over 30 years.
There's a whole chapter dedicated to it in the Gang of Four Patterns book, the same book defines the Strategy Pattern https://en.wikipedia.org/wi…