prepare for client/server split

This commit is contained in:
Morten Olsen
2024-12-11 12:32:23 +01:00
parent 02614d216c
commit 99248e98b5
51 changed files with 409 additions and 1500 deletions

View File

@@ -1 +1,2 @@
export { MarkdownPlugin } from './markdown.js';
export { markdownPlugin } from './markdown.js';
export { MarkdownPlugin } from './manifest.js';

View File

@@ -0,0 +1,14 @@
import { Manifest, z } from '@plainidx/plainidx';
const MarkdownPlugin = {
id: 'markdown',
name: 'Markdown Plugin',
version: '0.0.1',
config: z.any(),
backend: {
main: './dist/plugin.js',
actions: {},
},
} satisfies Manifest;
export { MarkdownPlugin };

View File

@@ -1,71 +1,59 @@
import { createActionApiRoute, Document, Plugin, z } from '@plainidx/plainidx';
import { MarkdownAst } from './utils/markdown-ast.js';
import { CorePlugin } from '@plainidx/plugin-core';
import { MarkdownPlugin } from './manifest.js';
import { MarkdownAst } from './utils/markdown-ast.js';
import { createPlugin } from '@plainidx/plainidx';
type MarkdownSubPlugin = {
process: (ast: MarkdownAst) => Promise<void>;
};
class MarkdownPlugin extends Plugin {
#subPlugins = new Set<MarkdownSubPlugin>();
public readonly name = '@builtin/markdown';
public actions = {
register: createActionApiRoute({
input: z.object({
plugin: z.custom<MarkdownSubPlugin>(),
}),
handle: async ({ plugin }) => {
this.#subPlugins.add(plugin);
},
}),
};
public process = async (document: Document) => {
if (!document.location.endsWith('.md')) {
return;
}
const ast = new MarkdownAst(document.data);
for (const plugin of this.#subPlugins) {
await plugin.process(ast);
}
const tags: string[] = [];
let topHead = Number.MAX_SAFE_INTEGER;
let title = 'Untitles';
ast.visit((node) => {
if (node.type !== 'heading') {
const markdownPlugin = createPlugin(MarkdownPlugin, ({ getPlugin }) => {
const plugins: MarkdownSubPlugin[] = [];
return {
actions: {},
process: async (document) => {
const core = await getPlugin(CorePlugin);
if (!document.location.endsWith('.md')) {
return;
}
if (node.depth === undefined) {
return;
}
if (node.depth < topHead) {
topHead = node.depth;
title = ast.nodeToString(node.children);
}
});
const ast = new MarkdownAst(document.data);
await this.action(CorePlugin, 'setTitle', {
title,
document: document.location,
});
ast.visit((node) => {
if (node.type === 'textDirective' && node.name === 'tag') {
const body = ast.nodeToString(node.children);
tags.push(body);
for (const plugin of plugins) {
await plugin.process(ast);
}
});
document.replace(ast.toBuffer());
await this.action(CorePlugin, 'setTags', {
tags,
document: document.location,
});
const tags: string[] = [];
let topHead = Number.MAX_SAFE_INTEGER;
let title = 'Untitles';
ast.visit((node) => {
if (node.type !== 'heading') {
return;
}
if (node.depth === undefined) {
return;
}
if (node.depth < topHead) {
topHead = node.depth;
title = ast.nodeToString(node.children);
}
});
ast.visit((node) => {
if (node.type === 'textDirective' && node.name === 'tag') {
const body = ast.nodeToString(node.children);
tags.push(body);
}
});
document.replace(ast.toBuffer());
await core.actions.setData({
location: document.location,
title,
tags,
});
},
};
}
});
export { MarkdownPlugin };
export { markdownPlugin };