-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
174 lines (149 loc) · 5.04 KB
/
Copy pathmain.js
File metadata and controls
174 lines (149 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Paragraph Numbers – Obsidian plugin
*
* Replaces line numbers with paragraph numbers in the editor gutter.
* A "paragraph" is a contiguous block of non-blank content lines,
* excluding YAML frontmatter, headings, code fences, and horizontal rules.
*/
const { Plugin } = require("obsidian");
// ── CodeMirror 6 imports (Obsidian re-exports them) ──────────────
const { StateField, StateEffect, RangeSet } = require("@codemirror/state");
const { gutter, GutterMarker } = require("@codemirror/view");
// ── GutterMarker that renders a paragraph number ─────────────────
class ParagraphNumberMarker extends GutterMarker {
constructor(num) {
super();
this.num = num;
}
toDOM() {
const span = document.createTextNode(String(this.num));
return span;
}
eq(other) {
return this.num === other.num;
}
}
// ── Classify a line ──────────────────────────────────────────────
// Returns: "blank" | "frontmatter" | "heading" | "codefence" | "hr" | "content"
function classifyLine(text, inFrontmatter, inCodeFence) {
const trimmed = text.trim();
if (inCodeFence) {
// Only a closing fence ends the code block
if (/^(`{3,}|~{3,})/.test(trimmed)) return "codefence";
return "codefence-inner";
}
if (inFrontmatter) {
if (trimmed === "---") return "frontmatter-end";
return "frontmatter";
}
if (trimmed === "") return "blank";
if (/^(`{3,}|~{3,})/.test(trimmed)) return "codefence";
if (/^#{1,6}\s/.test(trimmed)) return "heading";
if (/^(-{3,}|\*{3,}|_{3,})$/.test(trimmed)) return "hr";
return "content";
}
// ── Build paragraph map from document ────────────────────────────
// Returns Map<lineIndex, paragraphNumber> where lineIndex is 0-based
// Only the FIRST content line of each paragraph gets a number.
function buildParagraphMap(doc) {
const map = new Map();
let paraNum = 0;
let inFrontmatter = false;
let inCodeFence = false;
let inParagraph = false;
for (let i = 0; i < doc.lines; i++) {
const lineObj = doc.line(i + 1); // CM lines are 1-based
const text = lineObj.text;
const kind = classifyLine(text, inFrontmatter, inCodeFence);
// Track state transitions
if (i === 0 && text.trim() === "---") {
inFrontmatter = true;
continue;
}
if (kind === "frontmatter-end") {
inFrontmatter = false;
continue;
}
if (kind === "frontmatter") continue;
if (kind === "codefence") {
inCodeFence = !inCodeFence;
inParagraph = false;
continue;
}
if (kind === "codefence-inner") {
inParagraph = false;
continue;
}
if (kind === "blank" || kind === "heading" || kind === "hr") {
inParagraph = false;
continue;
}
// kind === "content"
if (!inParagraph) {
paraNum++;
map.set(i, paraNum);
inParagraph = true;
}
// Continuation lines within the same paragraph get no number
}
return map;
}
// ── StateField: recompute paragraph map on doc changes ───────────
const paragraphMapField = StateField.define({
create(state) {
return buildParagraphMap(state.doc);
},
update(value, tr) {
if (tr.docChanged) {
return buildParagraphMap(tr.state.doc);
}
return value;
},
});
// ── Gutter extension ─────────────────────────────────────────────
const paragraphGutter = gutter({
class: "cm-paragraph-number",
lineMarker(view, line) {
const map = view.state.field(paragraphMapField);
const lineIndex = view.state.doc.lineAt(line.from).number - 1; // 0-based
const num = map.get(lineIndex);
if (num !== undefined) {
return new ParagraphNumberMarker(num);
}
return null;
},
});
// ── Plugin class ─────────────────────────────────────────────────
const DEFAULT_SETTINGS = { enabled: true };
class ParagraphNumbersPlugin extends Plugin {
async onload() {
await this.loadSettings();
this.registerEditorExtension([paragraphMapField, paragraphGutter]);
this.applyEnabledClass();
this.addCommand({
id: "toggle-paragraph-numbers",
name: "Toggle paragraph numbers",
callback: async () => {
this.settings.enabled = !this.settings.enabled;
await this.saveSettings();
this.applyEnabledClass();
},
});
}
onunload() {
document.body.classList.remove("paragraph-numbers-hidden");
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
applyEnabledClass() {
document.body.classList.toggle(
"paragraph-numbers-hidden",
!this.settings.enabled,
);
}
}
module.exports = ParagraphNumbersPlugin;