Skip to content

Commit 4f008f7

Browse files
committed
getting the publish plugin running again
1 parent ab989c4 commit 4f008f7

22 files changed

+38786
-24593
lines changed

Publish.js

Lines changed: 38486 additions & 24138 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

automation/publishServer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Elysia } from 'elysia';
2+
3+
new Elysia().get('/', Bun.file('./Publish.js')).listen(31000);

bun.lockb

6.16 KB
Binary file not shown.

esbuild.publish.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ esbuild
5757
logLevel: 'info',
5858
sourcemap: false,
5959
treeShaking: true,
60+
minify: true,
6061
outfile: './Publish.js',
6162
define: {
6263
MB_GLOBAL_CONFIG_DEV_BUILD: 'false',

esbuild.publish.dev.config.mjs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import esbuild from 'esbuild';
2+
import process from 'process';
3+
import builtins from 'builtin-modules';
4+
import esbuildSvelte from 'esbuild-svelte';
5+
import sveltePreprocess from 'svelte-preprocess';
6+
import manifest from './manifest.json' assert { type: 'json' };
7+
8+
const banner = `/*
9+
-------------------------------------------
10+
${manifest.name} - Publish Build (based on ${manifest.version})
11+
-------------------------------------------
12+
By: ${manifest.author} (${manifest.authorUrl})
13+
Time: ${new Date().toUTCString()}
14+
Version: Publish Build (based on ${manifest.version})
15+
-------------------------------------------
16+
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
17+
if you want to view the source, please visit the github repository of this plugin
18+
*/
19+
`;
20+
21+
const context = await esbuild
22+
.context({
23+
banner: {
24+
js: banner,
25+
},
26+
entryPoints: ['src/publish/Publish.ts'],
27+
bundle: true,
28+
external: [
29+
'obsidian',
30+
'electron',
31+
'@codemirror/autocomplete',
32+
'@codemirror/closebrackets',
33+
'@codemirror/collab',
34+
'@codemirror/commands',
35+
'@codemirror/comment',
36+
'@codemirror/fold',
37+
'@codemirror/gutter',
38+
'@codemirror/highlight',
39+
'@codemirror/history',
40+
'@codemirror/language',
41+
'@codemirror/lint',
42+
'@codemirror/matchbrackets',
43+
'@codemirror/panel',
44+
'@codemirror/rangeset',
45+
'@codemirror/rectangular-selection',
46+
'@codemirror/search',
47+
'@codemirror/state',
48+
'@codemirror/stream-parser',
49+
'@codemirror/text',
50+
'@codemirror/tooltip',
51+
'@codemirror/view',
52+
...builtins,
53+
],
54+
format: 'iife',
55+
globalName: 'meta_bind_publish',
56+
target: 'es2016',
57+
logLevel: 'info',
58+
sourcemap: false,
59+
treeShaking: true,
60+
outfile: './Publish.js',
61+
define: {
62+
MB_GLOBAL_CONFIG_DEV_BUILD: 'false',
63+
},
64+
plugins: [
65+
esbuildSvelte({
66+
compilerOptions: { css: 'injected' },
67+
preprocess: sveltePreprocess(),
68+
filterWarnings: warning => {
69+
// we don't want warnings from node modules that we can do nothing about
70+
return !warning.filename.includes('node_modules');
71+
},
72+
}),
73+
],
74+
})
75+
.catch(() => process.exit(1));
76+
77+
await context.watch();

package.json

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"dev": "bun run esbuild.dev.config.mjs",
88
"build": "tsc -noEmit -skipLibCheck && bun run esbuild.config.mjs production",
9-
"dev-publish": "bun run esbuild.publish.config.mjs",
9+
"dev-publish": "bun run esbuild.publish.dev.config.mjs",
1010
"build-publish": "tsc -noEmit -skipLibCheck && node esbuild.publish.config.mjs production",
1111
"tsc": "tsc -noEmit -skipLibCheck",
1212
"test": "LOG_TESTS=false bun test",
@@ -18,7 +18,8 @@
1818
"types": "tsc -p \"./tsconfig.types.json\"",
1919
"check": "bun run format:check && bun run lint && bun run tsc && bun run test",
2020
"check:fix": "bun run format && bun run lint:fix && bun run tsc && bun run test",
21-
"release": "bun run automation/release.ts"
21+
"release": "bun run automation/release.ts",
22+
"serve-publish": "bun --watch automation/publishServer.ts"
2223
},
2324
"keywords": [],
2425
"author": "Moritz Jung",
@@ -28,28 +29,29 @@
2829
"@codemirror/language": "6.9.2",
2930
"@codemirror/state": "6.3.1",
3031
"@codemirror/view": "6.22.0",
31-
"@happy-dom/global-registrator": "^13.3.1",
32-
"@tsconfig/svelte": "^5.0.0",
33-
"@types/bun": "^1.0.2",
34-
"@typescript-eslint/eslint-plugin": "^6.0.0",
35-
"@typescript-eslint/parser": "^6.0.0",
32+
"@happy-dom/global-registrator": "^13.3.8",
33+
"@tsconfig/svelte": "^5.0.2",
34+
"@types/bun": "^1.0.5",
35+
"@typescript-eslint/eslint-plugin": "^7.0.1",
36+
"@typescript-eslint/parser": "^7.0.1",
3637
"builtin-modules": "^3.3.0",
37-
"esbuild": "^0.19.5",
38-
"esbuild-plugin-copy-watch": "^2.0.0",
38+
"elysia": "^0.8.17",
39+
"esbuild": "^0.20.0",
40+
"esbuild-plugin-copy-watch": "^2.1.0",
3941
"esbuild-svelte": "^0.8.0",
40-
"eslint": "^8.52.0",
41-
"eslint-plugin-import": "^2.28.1",
42-
"eslint-plugin-isaacscript": "^3.5.6",
42+
"eslint": "^8.56.0",
43+
"eslint-plugin-import": "^2.29.1",
44+
"eslint-plugin-isaacscript": "^3.12.2",
4345
"eslint-plugin-only-warn": "^1.1.0",
4446
"obsidian": "latest",
4547
"obsidian-dataview": "0.5.56",
46-
"prettier": "^3.0.3",
47-
"prettier-plugin-svelte": "^3.0.3",
48+
"prettier": "^3.2.5",
49+
"prettier-plugin-svelte": "^3.2.1",
4850
"string-argv": "^0.3.2",
49-
"svelte": "^4.2.0",
50-
"svelte-preprocess": "^5.0.4",
51+
"svelte": "^4.2.11",
52+
"svelte-preprocess": "^5.1.3",
5153
"tslib": "2.6.2",
52-
"typescript": "^5.2.2"
54+
"typescript": "^5.3.3"
5355
},
5456
"dependencies": {
5557
"@codemirror/legacy-modes": "^6.3.3",

src/api/API.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { type FieldBase } from '../fields/IFieldBase';
1717
import { JsViewField } from '../fields/viewFields/JsViewField';
1818
import { InlineButtonBase } from '../fields/button/InlineButtonBase';
1919
import { ButtonBase } from '../fields/button/ButtonBase';
20+
import { ErrorLevel, MetaBindInternalError } from '../utils/errors/MetaBindErrors';
2021

2122
export enum FieldType {
2223
INPUT_FIELD = 'INPUT_FIELD',
@@ -90,4 +91,62 @@ export class API<Plugin extends IPlugin> {
9091
// TODO: Nice error message
9192
throw new Error(`Unknown field type: ${type}`);
9293
}
94+
95+
/**
96+
* Gets the prefix of a given widget type. (e.g. INPUT or VIEW)
97+
*
98+
* @param mdrcType
99+
*/
100+
public getInlineFieldDeclarationPrefix(mdrcType: FieldType): string {
101+
if (mdrcType === FieldType.INPUT_FIELD) {
102+
return 'INPUT';
103+
} else if (mdrcType === FieldType.VIEW_FIELD) {
104+
return 'VIEW';
105+
} else if (mdrcType === FieldType.INLINE_BUTTON) {
106+
return 'BUTTON';
107+
}
108+
109+
throw new MetaBindInternalError({
110+
errorLevel: ErrorLevel.CRITICAL,
111+
effect: 'failed to get declaration prefix',
112+
cause: `Invalid inline mdrc type "${mdrcType}"`,
113+
});
114+
}
115+
116+
/**
117+
* Checks if a string is a declaration of a given widget type.
118+
*
119+
* @param mdrcType
120+
* @param str
121+
*/
122+
public isInlineFieldDeclaration(mdrcType: FieldType, str: string): boolean {
123+
const startStr: string = this.getInlineFieldDeclarationPrefix(mdrcType) + '[';
124+
const endStr: string = ']';
125+
126+
return str.startsWith(startStr) && str.endsWith(endStr);
127+
}
128+
129+
/**
130+
* Checks if a string is any declaration and if yes returns the widget type.
131+
* This does not use {@link isInlineFieldDeclaration} because of performance reasons.
132+
*
133+
* @param str
134+
*/
135+
public isInlineFieldDeclarationAndGetType(str: string): FieldType | undefined {
136+
if (!str.endsWith(']')) {
137+
return undefined;
138+
}
139+
140+
for (const widgetType of Object.values(FieldType)) {
141+
if (!isFieldTypeAllowedInline(widgetType)) {
142+
continue;
143+
}
144+
const startStr: string = this.getInlineFieldDeclarationPrefix(widgetType) + '[';
145+
if (str.startsWith(startStr)) {
146+
return widgetType;
147+
}
148+
}
149+
150+
return undefined;
151+
}
93152
}

src/api/ObsidianAPI.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type MetaBindPlugin from '../main';
22
import { type Component } from 'obsidian';
3-
import { API, FieldType } from './API';
3+
import { API, FieldType, isFieldTypeAllowedInline } from './API';
44
import { ExcludedMDRC } from '../renderChildren/ExcludedMDRC';
55
import { type UnvalidatedInputFieldDeclaration } from '../parsers/inputFieldParser/InputFieldDeclaration';
66
import { Signal } from '../utils/Signal';
@@ -26,6 +26,8 @@ import {
2626
import { validateArgs } from '../utils/ZodUtils';
2727
import { InputFieldBase } from '../fields/inputFields/InputFieldBase';
2828
import { FieldMDRC } from '../renderChildren/FieldMDRC';
29+
import { MarkdownRenderChildWidget } from '../cm6/Cm6_Widgets';
30+
import { ErrorLevel, MetaBindInternalError } from '../utils/errors/MetaBindErrors';
2931

3032
export interface ComponentLike {
3133
addChild(child: Component): void;
@@ -338,4 +340,31 @@ export class ObsidianAPI extends API<MetaBindPlugin> {
338340

339341
return this.bindTargetParser.parseAndValidateBindTarget(fullDeclaration, currentFilePath);
340342
}
343+
344+
/**
345+
* Creates a MDRC widget from a given widget type.
346+
*
347+
* @param mdrcType
348+
* @param content
349+
* @param filePath
350+
* @param component
351+
* @param plugin
352+
*/
353+
public constructMDRCWidget(
354+
mdrcType: FieldType,
355+
content: string,
356+
filePath: string,
357+
component: Component,
358+
plugin: MetaBindPlugin,
359+
): MarkdownRenderChildWidget {
360+
if (isFieldTypeAllowedInline(mdrcType)) {
361+
return new MarkdownRenderChildWidget(mdrcType, content, filePath, component, plugin);
362+
}
363+
364+
throw new MetaBindInternalError({
365+
errorLevel: ErrorLevel.CRITICAL,
366+
effect: 'failed to construct mdrc',
367+
cause: `Invalid inline mdrc type "${mdrcType}"`,
368+
});
369+
}
341370
}

src/cm6/Cm5_Modes.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { type Mode, type StringStream } from 'codemirror';
22
import { yaml } from '@codemirror/legacy-modes/mode/yaml';
3-
import { MDRCWidgetUtils } from './MDRCWidgetUtils';
43
import type MetaBindPlugin from '../main';
54
import { SyntaxHighlighting } from '../parsers/syntaxHighlighting/SyntaxHighlighting';
65
import { type FieldType } from '../api/API';
@@ -71,7 +70,7 @@ export function registerCm5HLModes(plugin: MetaBindPlugin): void {
7170

7271
state.str = lines.filter(x => x.trim() !== '').join('\n');
7372

74-
let mdrcType = MDRCWidgetUtils.isDeclarationAndGetMDRCType(state.str.trim());
73+
let mdrcType = plugin.api.isInlineFieldDeclarationAndGetType(state.str.trim());
7574
if (mdrcType === undefined) {
7675
state.highlights = new SyntaxHighlighting(state.str, []);
7776
} else {

src/cm6/Cm6_ViewPlugin.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { type SyntaxNode } from '@lezer/common';
55
import { Component, editorLivePreviewField, type TFile } from 'obsidian';
66
import type MetaBindPlugin from '../main';
77
import { Cm6_Util, MB_WidgetType } from './Cm6_Util';
8-
import { MDRCWidgetUtils } from './MDRCWidgetUtils';
98
import { summary } from 'itertools-ts/es';
109
import { type FieldType } from '../api/API';
1110

@@ -235,7 +234,7 @@ export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlug
235234
const content = Cm6_Util.getContent(view.state, from, to);
236235
return {
237236
content: content,
238-
widgetType: MDRCWidgetUtils.isDeclarationAndGetMDRCType(content),
237+
widgetType: plugin.api.isInlineFieldDeclarationAndGetType(content),
239238
};
240239
}
241240

@@ -318,7 +317,7 @@ export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlug
318317
currentFile: TFile,
319318
): Range<Decoration> | Range<Decoration>[] {
320319
if (widgetType === MB_WidgetType.FIELD) {
321-
const widget = MDRCWidgetUtils.constructMDRCWidget(
320+
const widget = plugin.api.constructMDRCWidget(
322321
mdrcType,
323322
content,
324323
currentFile.path,

0 commit comments

Comments
 (0)