Skip to content

Commit a3ca896

Browse files
committed
fix: unassigning code for multi-line paragraphs (#15)
1 parent 89310bc commit a3ca896

3 files changed

Lines changed: 35 additions & 15 deletions

File tree

src/coding/unassign-code.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { incrementProgress } from "src/auxiliary/progress-tracker";
33
import type Quadro from "src/main";
44
import { BLOCKID_REGEX } from "src/shared/add-blockid-to-datafile";
55
import { ExtendedFuzzySuggester } from "src/shared/modals";
6-
import { getActiveEditor, typeOfFile, WIKILINK_REGEX } from "src/shared/utils";
6+
import {
7+
getActiveEditor,
8+
moveToLastLineOfParagraph,
9+
typeOfFile,
10+
WIKILINK_REGEX,
11+
} from "src/shared/utils";
712
import { type Code, codeFileDisplay, getCodesFilesInParagraphOfDatafile } from "./coding-utils";
813

914
//──────────────────────────────────────────────────────────────────────────────
@@ -119,8 +124,9 @@ export function unassignCodeCommand(plugin: Quadro): void {
119124
return;
120125
}
121126

122-
const paragraphText = editor.getLine(editor.getCursor().line);
123-
const codesInPara = getCodesFilesInParagraphOfDatafile(plugin, dataFile, paragraphText);
127+
const lnum = moveToLastLineOfParagraph(editor); // to remove from multiline paragraphs, see #15
128+
const lineText = editor.getLine(lnum);
129+
const codesInPara = getCodesFilesInParagraphOfDatafile(plugin, dataFile, lineText);
124130

125131
if (codesInPara.length === 0) {
126132
new Notice("Line does not contain any codes to remove.", 3500);

src/shared/add-blockid-to-datafile.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type Editor, moment, type TFile } from "obsidian";
2+
import { moveToLastLineOfParagraph } from "./utils";
23

34
// DOCS https://help.obsidian.md/Linking+notes+and+files/Internal+links#Link+to+a+block+in+a+note
45
export const BLOCKID_REGEX = /\^[\w-]+$/;
@@ -18,14 +19,7 @@ export function prepareDatafileLineUpdate(editor: Editor): {
1819
// 2. move cursor to last line of paragraph
1920
// (This is relevant since when there are soft line breaks in a paragraph,
2021
// block-ids are only valid on the last line of the paragraph, see #12.)
21-
let lnum: number;
22-
while (true) {
23-
lnum = editor.getCursor().line;
24-
const nextLineIsBlank = editor.getLine(lnum + 1).trim() === "";
25-
const currentLineIsListItem = editor.getLine(lnum).match(/^\s*([-*+]|[0-9]+\.)\s/);
26-
if (nextLineIsBlank || currentLineIsListItem) break;
27-
editor.setCursor({ line: lnum + 1, ch: 0 });
28-
}
22+
const lnum = moveToLastLineOfParagraph(editor);
2923

3024
// 3. get existing blockID or generate new one
3125
const lineText = editor.getLine(lnum);

src/shared/utils.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@ export const WIKILINK_REGEX = /\[\[(.+?)([|#].*?)?\]\]/;
2121
export function typeOfFile(
2222
plugin: Quadro,
2323
file?: TAbstractFile | string | null,
24-
): "Data File" | "Code File" | "Extraction File" | "Template" | "Backup" | "Not Markdown" {
24+
):
25+
| "Data File"
26+
| "Code File"
27+
| "Extraction File"
28+
| "Template"
29+
| "Backup"
30+
| "Not Markdown"
31+
| "No File" {
2532
const { app, settings } = plugin;
26-
2733
if (!file) file = app.workspace.getActiveFile();
2834
if (typeof file === "string") file = app.vault.getFileByPath(file);
2935

30-
if (!file || !(file instanceof TFile) || file.extension !== "md") return "Not Markdown";
31-
36+
if (!file) return "No File";
37+
if (!(file instanceof TFile) || file.extension !== "md") return "Not Markdown";
3238
if (file.name === "Template.md") return "Template";
3339
if (file.path.includes(BACKUP_DIRNAME)) return "Backup";
3440
if (file.path.startsWith(settings.coding.folder + "/")) return "Code File";
@@ -112,6 +118,20 @@ export function activeFileHasInvalidName(app: App): boolean {
112118
return true;
113119
}
114120

121+
/** needed, since Obsidian block-ids are only valid on the last line of a paragraph */
122+
export function moveToLastLineOfParagraph(editor: Editor): number {
123+
let lnum = editor.getCursor().line;
124+
while (true) {
125+
// beyond end of file, `editor.getLine` also returns "", thus this check suffices
126+
const nextLineIsBlank = editor.getLine(lnum + 1).trim() === "";
127+
const currentLineIsListItem = editor.getLine(lnum).match(/^\s*([-*+]|[0-9]+\.)\s/);
128+
if (nextLineIsBlank || currentLineIsListItem) break;
129+
lnum++;
130+
editor.setCursor({ line: lnum, ch: 0 });
131+
}
132+
return lnum;
133+
}
134+
115135
//──────────────────────────────────────────────────────────────────────────────
116136

117137
/** Changed types breaks some things, such as the display of dates in

0 commit comments

Comments
 (0)