From 06e1b64bf6db598fa1a7172db78487dd4922c062 Mon Sep 17 00:00:00 2001 From: masaki39 Date: Wed, 8 May 2024 00:13:20 +0900 Subject: [PATCH] add insert method --- README.md | 52 ++++++++++++++++++++++++++++++++++++++++++--------- main.ts | 39 ++++++++++++++++++++++++++------------ manifest.json | 2 +- 3 files changed, 71 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 6e86bd0..dfaf87f 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,9 @@ - # Obsidian Outline Converter Plugin - Obsidian Outline Converter Plugin - Features - This plugin converts bullet list outlines into continuous text. - You can use two commands: `Auto-header converter` & `Custom converter` - - It includes commands to fold text at indentation levels 1-3. - Installation - You can download from `Community plugins` in Obsidian settings. - Commands @@ -17,27 +15,44 @@ - Options include inserting text before or after content, ignoring content and using line breaks. - Replacement - You can search & replace terms as you like before outputting the connected sentences. + - {{break}} + - Double line breaks are inserted before this sentence, as I configured. + - Insertion + - You can use `{{i:id}}` as insert anything in the active file. + - {{i:mermaid}} + - `{{i:id}}` is replaced with content between `{{s:id}}` and `{{e:id}}`. - Select export method - Choose from the following options for exporting your text: - Copy to clipboard. - Append to the cursor's current position. - Append to the bottom of the active note. - Replace a section; if none exists, create it at the bottom of the note. - - Swap lines commands - - In bullet lists, it uses line swapping commands of the Outliner plugin. - - Outside of that, it simply swaps lines. - - You cannot use these commands unless the Outliner plugin is installed. + - Other commands + - Fold all of indentation levels 1-3. + - Swap lines commands + - In bullet lists, it uses line swapping commands of the Outliner plugin. + - Outside of that, it simply swaps lines. - Note - The command of `Auto-header converter` can convert this outline into the text below. +{{s:mermaid}} + +```mermaid +flowchart TD +OutlineList-->|convert|ContinuousText +ContinuousText-->|replacement×1-5|ReplacedText +ReplacedText-->|+insertion|Export +``` +{{e:mermaid}} # Output + ## Obsidian Outline Converter Plugin ### Features -This plugin converts bullet list outlines into continuous text. You can use two commands: `Auto-header converter` & `Custom converter` It includes commands to fold text at indentation levels 1-3. +This plugin converts bullet list outlines into continuous text. You can use two commands: `Auto-header converter` & `Custom converter` ### Installation @@ -59,13 +74,32 @@ Customize how text is handled at each indentation level from 1 to 5. Options inc You can search & replace terms as you like before outputting the connected sentences. + Double line breaks are inserted before this sentence, as I configured. + +### Insertion + +You can use `{{i:id}}` as insert anything in the active file. + +```mermaid +flowchart TD +OutlineList-->|convert|ContinuousText +ContinuousText-->|replacement×1-5|ReplacedText +ReplacedText-->|+insertion|Export +``` + + Some + ### Select export method Choose from the following options for exporting your text: Copy to clipboard. Append to the cursor's current position. Append to the bottom of the active note. Replace a section; if none exists, create it at the bottom of the note. -### Swap lines commands +### Other commands + +Fold all of indentation levels 1-3. + +#### Swap lines commands -In bullet lists, it uses line swapping commands of the Outliner plugin. Outside of that, it simply swaps lines. You cannot use these commands unless the Outliner plugin is installed. +In bullet lists, it uses line swapping commands of the Outliner plugin. Outside of that, it simply swaps lines. ### Note diff --git a/main.ts b/main.ts index bc07a69..cb0ae44 100644 --- a/main.ts +++ b/main.ts @@ -98,6 +98,9 @@ export default class OutlineConverter extends Plugin { // transform linebreak result = result.replace(/\\n/g, "\n"); + //insert methods + result = await this.insertContent(result); + // export if (this.settings.exportMethod == 'Copy'){ this.copyContent(result); @@ -189,6 +192,9 @@ export default class OutlineConverter extends Plugin { // transform linebreak result = result.replace(/\\n/g, "\n"); + //insert methods + result = await this.insertContent(result); + // export if (this.settings.exportMethod == 'Copy'){ this.copyContent(result); @@ -287,11 +293,6 @@ export default class OutlineConverter extends Plugin { const commands = (this.app as any).commands; const commandExist = commands.listCommands().some((cmd: any) => cmd.id === commandId); - if (!commandExist){ - new Notice("Install outliner plugin"); - return; - } - const cursor = editor.getCursor(); if (cursor.line == 0) { return; @@ -300,7 +301,7 @@ export default class OutlineConverter extends Plugin { const line = editor.getLine(cursor.line); const lineAbove = editor.getLine(cursor.line - 1); - if (line.trim().startsWith(`- `) && lineAbove.trim().startsWith(`- `)) { + if (commandExist && !editor.somethingSelected() && line.trim().startsWith(`- `) && lineAbove.trim().startsWith(`- `)) { commands.executeCommandById(commandId); } else { editor.exec("swapLineUp"); @@ -319,11 +320,6 @@ export default class OutlineConverter extends Plugin { const commands = (this.app as any).commands; const commandExist = commands.listCommands().some((cmd: any) => cmd.id === commandId); - if (!commandExist){ - new Notice("Install outliner plugin"); - return; - } - const cursor = editor.getCursor(); const lastLineNumber = editor.lineCount() - 1; // Get the index of the last line @@ -335,7 +331,7 @@ export default class OutlineConverter extends Plugin { const lineBelow = editor.getLine(cursor.line + 1); // Check if both current line and the line below start with "- " - if (line.trim().startsWith(`- `) && lineBelow.trim().startsWith(`- `)) { + if (commandExist && !editor.somethingSelected() && line.trim().startsWith(`- `) && lineBelow.trim().startsWith(`- `)) { commands.executeCommandById(commandId); } else { editor.exec("swapLineDown"); @@ -466,6 +462,25 @@ export default class OutlineConverter extends Plugin { return connectedResult; } + // insert methods + async insertContent(result: string): Promise { + // get file + const activeFile = this.app.workspace.getActiveFile(); + if (!activeFile) { + return result; + } + const fileContent = await this.app.vault.read(activeFile); + + // match {{i:id}} + const insertPattern = /\{\{i:(.+?)\}\}/g; + result = result.replace(insertPattern, (match, id) => { + const contentPattern = new RegExp(`\\{\\{s:${id}\\}\\}(.*?)\\{\\{e:${id}\\}\\}`, 's'); + const content = contentPattern.exec(fileContent); + return content ? content[1] : match; + }); + return result; + } + // add output methods // copy content diff --git a/manifest.json b/manifest.json index 9cd0681..56094e9 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "outline-converter", "name": "Outline Converter", - "version": "1.2.0", + "version": "1.3.0", "minAppVersion": "1.5.12", "description": "Convert outline to continuous text.", "author": "masaki39",