-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
234 lines (195 loc) · 7.54 KB
/
main.ts
File metadata and controls
234 lines (195 loc) · 7.54 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { App, Editor, MarkdownView, Notice, Plugin, PluginSettingTab, Setting } from "obsidian";
interface RemoveListBlankLinesSettings {
strictInListOnly: boolean;
}
const DEFAULT_SETTINGS: RemoveListBlankLinesSettings = {
strictInListOnly: true
};
// Detect list items: bullets (- + *), tasks (- [ ] etc.), and ordered (1. / 1) )
const listItemRegex = /^\s*(?:[-+*]\s+(?:\[[ xX]\]\s+)?|\d+[.)]\s+)/;
// Whitespace-only line (spaces/tabs only, possibly length 0)
const whitespaceOnlyRegex = /^[ \t]*$/;
function isListItem(line: string): boolean {
return listItemRegex.test(line);
}
// True if the line is blank and contains at least one space or tab
function isIndentedBlank(line: string): boolean {
if (!whitespaceOnlyRegex.test(line)) return false;
// Line length > 0 implies at least one space/tab (since only [ \t]* allowed)
return line.length > 0;
}
/**
* Remove blank lines between list items.
* Strict mode:
* - Between two list items: remove only indented blanks; keep one pure empty to separate distinct lists.
* - After the last item of a list:
* - If next non-blank starts another list: remove indented blanks; keep exactly one pure empty as separator.
* - If end of document: remove indented blanks; keep no trailing pure empty.
* Non-strict mode:
* - Remove all blank lines between list items (merges lists) and drop trailing blanks after a list item.
*/
function removeBlankLinesBetweenListItems(text: string, strictInListOnly: boolean): string {
const lines = text.split("\n");
const out: string[] = [];
let blankBuffer: string[] = [];
let prevNonBlank: string | null = null; // last emitted non-blank line
const flushBlankBuffer = () => {
for (const b of blankBuffer) out.push(b);
blankBuffer = [];
};
const dropBlankBuffer = () => {
blankBuffer = [];
};
// Helper to decide the final flushing at EOF according to strict/non-strict mode
const flushTail = () => {
if (blankBuffer.length === 0) return;
const prevIsList = prevNonBlank !== null ? isListItem(prevNonBlank) : false;
if (!prevIsList) {
// Previous content is not a list item; keep blanks as-is
flushBlankBuffer();
return;
}
if (!strictInListOnly) {
// Non-strict: drop all trailing blanks after a list item
dropBlankBuffer();
return;
}
// Strict:
// At EOF, there is no "next list", so we must drop indented blanks and keep no trailing pure empty.
const hasIndented = blankBuffer.some(isIndentedBlank);
if (hasIndented) {
// Remove all indented blanks; at EOF we do not keep any pure empty separator either.
dropBlankBuffer();
return;
}
// Only pure empty blanks remain. At EOF, keep none.
dropBlankBuffer();
};
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
// Buffer whitespace-only lines until we see what follows
if (whitespaceOnlyRegex.test(line)) {
blankBuffer.push(line);
continue;
}
// We have a non-blank current line; decide what to do with the buffered blanks
const currIsList = isListItem(line);
const prevIsList = prevNonBlank !== null ? isListItem(prevNonBlank) : false;
if (blankBuffer.length > 0) {
if (prevIsList && currIsList) {
// Blanks are between two list items
if (!strictInListOnly) {
// Merge: drop all blanks
dropBlankBuffer();
} else {
// Strict: only drop if at least one is an indented blank
const anyIndented = blankBuffer.some(isIndentedBlank);
const hasPureEmpty = blankBuffer.some(b => b === "");
if (anyIndented) {
// Drop all indented blanks but preserve one pure empty if present to keep lists separated
dropBlankBuffer();
if (hasPureEmpty) {
blankBuffer = [""];
flushBlankBuffer();
}
} else {
// Pure empty separator -> keep exactly one blank line to separate lists
blankBuffer = [""];
flushBlankBuffer();
}
}
} else if (prevIsList && !currIsList) {
// Trailing blanks after a list item before non-list content
if (!strictInListOnly) {
// Non-strict: drop them
dropBlankBuffer();
} else {
// Strict: drop indented blanks; keep one pure empty if any
const hasPureEmpty = blankBuffer.some(b => b === "");
const hasIndented = blankBuffer.some(isIndentedBlank);
if (hasIndented) {
dropBlankBuffer();
if (hasPureEmpty) {
blankBuffer = [""];
flushBlankBuffer();
}
} else {
// Only pure empty -> normalize to a single empty line
blankBuffer = [""];
flushBlankBuffer();
}
}
} else if (!prevIsList && currIsList) {
// Blanks before a list start from non-list content: keep as-is (they separate blocks)
flushBlankBuffer();
} else {
// Neither side is a list item: keep as-is
flushBlankBuffer();
}
}
// Emit current line
out.push(line);
prevNonBlank = line;
}
// End-of-document tail handling
flushTail();
return out.join("\n");
}
export default class RemoveListBlankLinesPlugin extends Plugin {
settings: RemoveListBlankLinesSettings = DEFAULT_SETTINGS;
async onload() {
await this.loadSettings();
this.addCommand({
id: "remove-blank-lines-between-list-items",
name: "Remove blank lines between list items",
editorCallback: (editor: Editor, ctx) => {
if (!(ctx instanceof MarkdownView)) return;
const doc = editor.getValue();
const cleaned = removeBlankLinesBetweenListItems(doc, this.settings.strictInListOnly);
if (doc !== cleaned) {
const cursor = editor.getCursor();
editor.setValue(cleaned);
editor.setCursor(cursor);
new Notice("Removed blank lines between list items");
} else {
new Notice("No changes");
}
}
});
this.addSettingTab(new RemoveListBlankLinesSettingTab(this.app, this));
}
onunload() {}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class RemoveListBlankLinesSettingTab extends PluginSettingTab {
plugin: RemoveListBlankLinesPlugin;
constructor(app: App, plugin: RemoveListBlankLinesPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName("Cleaning method")
.setHeading();
new Setting(containerEl)
.setName("Strict in-list only")
.setDesc(
"If enabled, remove only indented blank lines between list items; keep a single pure empty line to separate lists. Also remove trailing indented blanks after a list; at end-of-file no trailing blank line is kept. If disabled, remove all blanks between list items and trailing after lists."
)
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.strictInListOnly)
.onChange(async (value) => {
this.plugin.settings.strictInListOnly = value;
await this.plugin.saveSettings();
});
});
}
}