Skip to content

Commit 793ece3

Browse files
committed
update syntax highlighting
1 parent f5bd6d9 commit 793ece3

File tree

10 files changed

+114
-104
lines changed

10 files changed

+114
-104
lines changed

exampleVault/Meta Bind JS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
select: c
2+
select: d
33
---
44

55
test

exampleVault/examples.md

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ suggest: test
66
toggle1: false
77
Domestic_tasks:
88
- Lunch 🍲
9+
Meditate: 100
10+
Slept: 00:00
911
---
1012

1113
## In callouts
1214
> quote
1315
> `INPUT[slider(addLabels, minValue(1), maxValue(10)):slider1]`
1416
1517
> [!INFO]
16-
> `INPUT[slider(addLabels, minValue(1), maxValue(10)):slider1]` Slider
18+
> `INPUT[slider(addLabels, minValue(1), maxValue(10)):slider1]`
1719
1820
> [!INFO]
19-
> `INPUT[slider(addLabels, minValue(1), maxValue(10)):slider1]`
21+
> ```meta-bind
22+
> INPUT[slider(addLabels, minValue(1), maxValue(10)):slider1]
23+
> ```
2024
2125
## Nested data
2226
`INPUT[text:nested["object"]]`
@@ -71,4 +75,40 @@ Lorem ipsum dolor sit amet, `INPUT[date():other note#date]` consectetur adipisci
7175
Code block error
7276
```meta-bind
7377
INPUT[slider(nonExistantArgument)]
74-
```
78+
```
79+
80+
81+
82+
83+
84+
>[!info]+ Trackers
85+
>`````col
86+
>flexgrow=2
87+
>===
88+
>````col-md
89+
>##### ○ Slept
90+
>```meta-bind
91+
>INPUT[time(class(meta-bind-full-width)):Slept]
92+
>```
93+
>````
94+
>
95+
>````col-md
96+
>##### ○ Woke Up
97+
>```meta-bind
98+
>INPUT[time(class(meta-bind-full-width)):WokeUp]
99+
>```
100+
>````
101+
>````col-md
102+
>##### ○ Stretch (minutes)
103+
>```meta-bind
104+
>INPUT[number(class(meta-bind-full-width)):Stretch]
105+
>```
106+
>````
107+
>
108+
>````col-md
109+
>##### ○ Meditate (minutes)
110+
>```meta-bind
111+
>INPUT[number(class(meta-bind-full-width)):Meditate]
112+
>```
113+
>````
114+
>`````

package-lock.json

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
"author": "Moritz Jung",
1616
"license": "GPL-3.0",
1717
"devDependencies": {
18+
"@codemirror/lang-javascript": "^6.1.2",
19+
"@codemirror/view": "^6.7.2",
1820
"@tsconfig/svelte": "^3.0.0",
1921
"@types/jest": "^28.1.6",
2022
"@types/node": "^16.11.6",
2123
"@typescript-eslint/eslint-plugin": "^5.30.0",
2224
"@typescript-eslint/parser": "^5.30.0",
23-
"@codemirror/view": "^6.7.2",
24-
"@codemirror/lang-javascript": "^6.1.2",
2525
"builtin-modules": "^3.3.0",
2626
"esbuild": "^0.14.47",
2727
"esbuild-plugin-copy-watch": "^0.0.7",

src/InputFieldMarkdownRenderChild.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
243243
this.plugin.unregisterInputFieldMarkdownRenderChild(this);
244244
this.unregisterSelfFromMetadataManager();
245245

246+
this.containerEl.empty();
247+
this.containerEl.remove();
248+
246249
super.onunload();
247250
}
248251
}

src/frontmatterDisplay/custom_overlay.js

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,25 @@
1717

1818
CodeMirror.customOverlayMode = function (base, overlay, combine) {
1919
return {
20-
startState: function () {
21-
return {
22-
base: CodeMirror.startState(base),
23-
overlay: CodeMirror.startState(overlay),
24-
basePos: 0,
25-
baseCur: null,
26-
overlayPos: 0,
27-
overlayCur: null,
28-
streamSeen: null,
29-
};
30-
},
31-
copyState: function (state) {
32-
return {
33-
base: CodeMirror.copyState(base, state.base),
34-
overlay: CodeMirror.copyState(overlay, state.overlay),
35-
basePos: state.basePos,
36-
baseCur: null,
37-
overlayPos: state.overlayPos,
38-
overlayCur: null,
39-
};
40-
},
20+
startState: () => ({
21+
base: CodeMirror.startState(base),
22+
overlay: CodeMirror.startState(overlay),
23+
basePos: 0,
24+
baseCur: null,
25+
overlayPos: 0,
26+
overlayCur: null,
27+
streamSeen: null,
28+
}),
29+
copyState: state => ({
30+
base: CodeMirror.copyState(base, state.base),
31+
overlay: CodeMirror.copyState(overlay, state.overlay),
32+
basePos: state.basePos,
33+
baseCur: null,
34+
overlayPos: state.overlayPos,
35+
overlayCur: null,
36+
}),
4137

42-
token: function (stream, state) {
38+
token: (stream, state) => {
4339
if (stream != state.streamSeen || Math.min(state.basePos, state.overlayPos) < stream.start) {
4440
state.streamSeen = stream;
4541
state.basePos = state.overlayPos = stream.start;
@@ -56,39 +52,25 @@
5652
}
5753
stream.pos = Math.min(state.basePos, state.overlayPos);
5854

59-
// // Edge case for codeblocks in templater mode
60-
// if (
61-
// state.baseCur &&
62-
// state.overlayCur &&
63-
// state.baseCur.contains("line-HyperMD-codeblock")
64-
// ) {
65-
// state.overlayCur = state.overlayCur.replace(
66-
// "line-templater-inline",
67-
// ""
68-
// );
69-
// state.overlayCur += ` line-background-HyperMD-codeblock-bg`;
70-
// }
71-
7255
// state.overlay.combineTokens always takes precedence over combine,
7356
// unless set to null
74-
if (state.overlayCur == null) return state.baseCur;
75-
else if ((state.baseCur != null && state.overlay.combineTokens) || (combine && state.overlay.combineTokens == null)) return state.baseCur + ' ' + state.overlayCur;
76-
else return state.overlayCur;
57+
if (state.overlayCur == null) {
58+
return state.baseCur;
59+
} else if ((state.baseCur != null && state.overlay.combineTokens) || (combine && state.overlay.combineTokens == null)) {
60+
return state.baseCur + ' ' + state.overlayCur;
61+
} else {
62+
return state.overlayCur;
63+
}
7764
},
7865

79-
indent:
80-
base.indent &&
81-
function (state, textAfter, line) {
82-
return base.indent(state.base, textAfter, line);
83-
},
66+
indent: base.indent && ((state, textAfter, line) => base.indent(state.base, textAfter, line)),
67+
8468
electricChars: base.electricChars,
8569

86-
innerMode: function (state) {
87-
return { state: state.base, mode: base };
88-
},
70+
innerMode: state => ({ state: state.base, mode: base }),
8971

90-
blankLine: function (state) {
91-
var baseToken, overlayToken;
72+
blankLine: state => {
73+
let baseToken, overlayToken;
9274
if (base.blankLine) baseToken = base.blankLine(state.base);
9375
if (overlay.blankLine) overlayToken = overlay.blankLine(state.overlay);
9476

src/inputFieldArguments/AbstractInputFieldArgument.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export abstract class AbstractInputFieldArgument {
77
requiresValue: boolean = false;
88
allowMultiple: boolean = false;
99

10-
abstract parseValue(valueStr: string): void;
10+
abstract parseValue(value: any): void;
1111

1212
isAllowed(inputFieldType: InputFieldType): boolean {
1313
if (this.allowedInputFields.length === 0) {

src/inputFieldArguments/AddLabelsInputFieldArgument.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ export class AddLabelsInputFieldArgument extends AbstractInputFieldArgument {
88
requiresValue: boolean = false;
99
allowMultiple: boolean = false;
1010

11-
parseValue(valueStr: string): void {
12-
this.value = valueStr.toLowerCase() === 'true';
11+
override parseValue(value: any): void {
12+
if (typeof value === 'boolean') {
13+
this.value = value;
14+
} else if (typeof value === 'string') {
15+
this.value = value.toLowerCase() === 'true';
16+
}
1317
}
1418
}

src/main.ts

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ export default class MetaBindPlugin extends Plugin {
9393

9494
this.app.workspace.onLayoutReady(async () => {
9595
await this.registerCodeMirrorMode();
96-
const mode = window.CodeMirror.getMode({}, { name: 'meta-bind-js' });
97-
this.registerEditorExtension(StreamLanguage.define(mode as any));
9896
});
9997

10098
this.addSettingTab(new MetaBindSettingTab(this.app, this));
@@ -104,76 +102,48 @@ export default class MetaBindPlugin extends Plugin {
104102
* Inspired by https://github.com/SilentVoid13/Templater/blob/487805b5ad1fd7fbc145040ed82b4c41fc2c48e2/src/editor/Editor.ts#L67
105103
*/
106104
async registerCodeMirrorMode(): Promise<void> {
107-
let js_mode: Mode<any> = window.CodeMirror.getMode({}, 'javascript');
105+
const js_mode: Mode<any> = window.CodeMirror.getMode({}, 'javascript');
108106
if (js_mode == null || js_mode.name === 'null') {
109107
console.log("Couldn't find js mode, can't enable syntax highlighting.");
110108
return;
111109
}
112110

113-
console.log(js_mode);
114-
115-
// Custom overlay mode used to handle edge cases
116-
// @ts-ignore
117-
const overlay_mode = window.CodeMirror.customOverlayMode;
118-
if (overlay_mode == null) {
119-
console.log("Couldn't find customOverlayMode, can't enable syntax highlighting.");
120-
return;
121-
}
122-
123111
// if templater enabled, this only runs on the code blocks, otherwise this runs on the whole document
124112

125-
window.CodeMirror.defineMode('meta-bind-js', function (config) {
113+
window.CodeMirror.defineMode('meta-bind-js', config => {
126114
const mbOverlay: any = {
127115
startState: () => {
128116
const js_state = window.CodeMirror.startState(js_mode);
129117
return {
130118
...js_state,
131-
inMBScriptCodeBlock: false,
132119
};
133120
},
134121
blankLine: (state: any) => {
135-
console.log(state, 'blank');
136122
return null;
137123
},
138124
copyState: (state: any) => {
139125
const js_state = window.CodeMirror.startState(js_mode);
140126
return {
141127
...js_state,
142-
inMBScriptCodeBlock: state.inMBScriptCodeBlock,
143128
};
144129
},
145130
token: (stream: any, state: any) => {
146-
const globals = ['app', 'mb', 'dv', 'filePath', 'ctx'];
147-
148-
console.log(stream);
149-
150-
if (state.inMBScriptCodeBlock) {
151-
if (stream.match(/```/, true)) {
152-
state.inMBScriptCodeBlock = false;
153-
} else {
154-
for (const global of globals) {
155-
if (stream.match(global)) {
156-
return 'atom';
157-
}
158-
}
159-
160-
const js_result = js_mode.token && js_mode.token(stream, state);
161-
return `line-HyperMD-codeblock ${js_result}`;
162-
}
163-
}
164-
165-
const match = stream.match(/```meta-bind-js/, true);
166-
console.log(match);
167-
if (match != null) {
168-
state.inMBScriptCodeBlock = true;
169-
}
170-
171-
while (stream.next() != null && !stream.match(/```meta-bind-js/, false));
172-
return null;
131+
// const globals = ['app', 'mb', 'dv', 'filePath', 'ctx'];
132+
133+
// console.log(stream);
134+
135+
// for (const global of globals) {
136+
// if (stream.match(global)) {
137+
// return 'variable';
138+
// }
139+
// }
140+
141+
const js_result = js_mode.token && js_mode.token(stream, state);
142+
return `line-HyperMD-codeblock ${js_result}`;
173143
},
174144
};
175145

176-
return overlay_mode(window.CodeMirror.getMode(config, 'hypermd'), mbOverlay);
146+
return mbOverlay;
177147
});
178148
}
179149

src/parsers/InputFieldDeclarationParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export class InputFieldDeclarationParser {
229229
return this.parseArguments(inputFieldType, inputFieldArguments);
230230
}
231231

232-
parseArguments(inputFieldType: InputFieldType, inputFieldArguments?: { type: InputFieldArgumentType; value: string }[] | undefined): InputFieldArgumentContainer {
232+
parseArguments(inputFieldType: InputFieldType, inputFieldArguments?: { type: InputFieldArgumentType; value: any }[] | undefined): InputFieldArgumentContainer {
233233
const argumentContainer: InputFieldArgumentContainer = new InputFieldArgumentContainer();
234234

235235
if (inputFieldArguments) {

0 commit comments

Comments
 (0)