Skip to content

Commit fe67cbb

Browse files
committed
Add header styling in source editor (closes #1)
1 parent ffe055d commit fe67cbb

File tree

6 files changed

+77
-7
lines changed

6 files changed

+77
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ This plugin doesn't support configuration at the moment.
1212

1313
- Preview of `org-mode` tasks (`TODO`, `DONE`, `COMPLETED`, etc.) with proper styling
1414
- Remove LogSeq timestamps from preview
15-
- Render `org-mode` blocks (eg `#+BEGIN_NOTE`/`#+END_NOTE`) as `<blockquote>`
15+
- Render `org-mode` blocks (eg `#+BEGIN_NOTE`/`#+END_NOTE`) as `<blockquote>`
16+
- Style headings in source editor

main.js

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

main.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ enum TaskType {
1414
UNKNOWN,
1515
}
1616

17-
const VERSION = "0.0.2";
17+
const HEADING_REGEX = {
18+
h1: /(?:\s+)?- # (?:.*)$/gms,
19+
h2: /(?:\s+)?- ## (?:.*)$/gms,
20+
h3: /(?:\s+)?- ### (?:.*)$/gms,
21+
h4: /(?:\s+)?- #### (?:.*)$/gms,
22+
h5: /(?:\s+)?- ##### (?:.*)$/gms,
23+
};
24+
25+
const VERSION = "0.0.3";
1826

1927
function parseTaskType(content: string): TaskType | null {
2028
if (content.startsWith("DONE ")) {
@@ -40,6 +48,10 @@ function removeTimestamps(content: string): string {
4048
.replace(/doing:: (?:\d{13})/gms, "")
4149
.replace(/later:: (?:\d{13})/gms, "")
4250
.replace(/canceled:: (?:\d{13})/gms, "")
51+
.replace(
52+
/id:: (?:[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12})/gims,
53+
""
54+
)
4355
.replace(/collapsed:: (?:true|false)/gms, "")
4456
.replace("<br>", "");
4557
}
@@ -50,6 +62,26 @@ function isBlock(content: string): boolean {
5062
return blockTest.test(content);
5163
}
5264

65+
function cmHeadingOverlay(cm: CodeMirror.Editor) {
66+
cm.addOverlay({
67+
token: (stream: any) => {
68+
if (stream.match(HEADING_REGEX["h1"])) {
69+
return "header-1";
70+
} else if (stream.match(HEADING_REGEX["h2"])) {
71+
return "header-2";
72+
} else if (stream.match(HEADING_REGEX["h3"])) {
73+
return "header-3";
74+
} else if (stream.match(HEADING_REGEX["h4"])) {
75+
return "header-4";
76+
} else if (stream.match(HEADING_REGEX["h5"])) {
77+
return "header-5";
78+
} else {
79+
stream.next();
80+
}
81+
},
82+
});
83+
}
84+
5385
export default class LogSeqPlugin extends Plugin {
5486
static postprocessor: MarkdownPostProcessor = (
5587
el: HTMLElement,
@@ -105,6 +137,8 @@ export default class LogSeqPlugin extends Plugin {
105137
onload() {
106138
console.log(`Loading LogSeq plugin ${VERSION}`);
107139
MarkdownPreviewRenderer.registerPostProcessor(LogSeqPlugin.postprocessor);
140+
// Style headings in source editing
141+
this.registerCodeMirror(cmHeadingOverlay);
108142
}
109143

110144
onunload() {

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "logseq-format",
33
"name": "LogSeq markdown formatting",
4-
"version": "0.0.2",
4+
"version": "0.0.3",
55
"minAppVersion": "0.9.15",
66
"description": "Render LogSeq-specific markdown",
77
"author": "Rui Vieira",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-plugin-logseq",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Render LogSeq-specific markdown.",
55
"main": "main.js",
66
"scripts": {

versions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"0.0.1": "0.9.15",
3-
"0.0.2": "0.9.15"
3+
"0.0.2": "0.9.15",
4+
"0.0.3": "0.9.15"
45
}

0 commit comments

Comments
 (0)