From 9c9e6ac6e1340a16eb498b1df321cd5fd2b3b9a6 Mon Sep 17 00:00:00 2001 From: valentine195 <38669521+valentine195@users.noreply.github.com> Date: Fri, 18 Feb 2022 09:03:38 -0500 Subject: [PATCH] feat: adds support for indented code blocks using MSDoc syntax (close #176) --- README.md | 13 +++++++++++++ src/@types/index.ts | 1 + src/main.ts | 17 +++++++++++++---- src/settings.ts | 29 +++++++++++++++++++++++++++++ src/util/constants.ts | 2 +- 5 files changed, 57 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 71b6076..b18404f 100644 --- a/README.md +++ b/README.md @@ -534,6 +534,15 @@ As of v6.8.0, an additional non-code block syntax can be used that is inspired b ![](https://raw.githubusercontent.com/valentine195/obsidian-admonition/master/images/msdocs.png) + +This syntax can also be used on indented code blocks: + +```md + [!quote] + This is an admonition! +``` + + ### Title A title can be added to the MSDoc-style admonition by appending it after the type. @@ -637,6 +646,10 @@ Enabled use of `!!! ad-` style admonitions. No longer supported, will be r Enables use of the [Microsoft Document Syntax](#microsoft-document-syntax) for blockquote admonitions. +### Use Microsoft Document Syntax for Indented Code Blocks + +Enables use of the [Microsoft Document Syntax](#microsoft-document-syntax) for indented code blocks. + ### Render Microsoft Document Syntax in Live Preview Enables use of the [Microsoft Document Syntax](#microsoft-document-syntax) in live preview. diff --git a/src/@types/index.ts b/src/@types/index.ts index 3f20a48..77541ef 100644 --- a/src/@types/index.ts +++ b/src/@types/index.ts @@ -34,6 +34,7 @@ export interface AdmonitionSettings { injectColor: boolean; parseTitles: boolean; allowMSSyntax: boolean; + msSyntaxIndented: boolean; livePreviewMS: boolean; dropShadow: boolean; hideEmpty: boolean; diff --git a/src/main.ts b/src/main.ts index 061882d..972a43f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -122,6 +122,7 @@ const DEFAULT_APP_SETTINGS: AdmonitionSettings = { injectColor: true, parseTitles: true, allowMSSyntax: true, + msSyntaxIndented: true, livePreviewMS: true, dropShadow: true, hideEmpty: false @@ -408,13 +409,21 @@ export default class ObsidianAdmonition extends Plugin { enableMSSyntax() { this.registerMarkdownPostProcessor((el, ctx) => { if (!this.data.allowMSSyntax) return; - if (el?.firstChild?.nodeName !== "BLOCKQUOTE") return; + if ( + el?.firstChild?.nodeName !== "BLOCKQUOTE" && + !( + el?.firstElementChild instanceof HTMLPreElement && + this.data.msSyntaxIndented + ) + ) + return; const section = ctx.getSectionInfo(el); if (!section) return; const text = section.text.split("\n"); const firstLine = text[section.lineStart]; - if (!/^> \[!.+\]/.test(firstLine)) return; + if (!MSDOCREGEX.test(firstLine)) return; + /* if (!/^(?:>[ ]|\t|\s{4})\[!.+\]/.test(firstLine)) return; */ const params = this.getMSParametersFromLine(firstLine); @@ -433,7 +442,7 @@ export default class ObsidianAdmonition extends Plugin { const content = text .slice(section.lineStart + 1, section.lineEnd + 1) .join("\n") - .replace(/> /g, ""); + .replace(/^(>[ ]|\t|\s{4})/gm, ""); const contentEl = this.getAdmonitionContentElement( type, @@ -639,7 +648,7 @@ export default class ObsidianAdmonition extends Plugin { view.state.doc.sliceString(from); const split = original.split("\n"); const line = split[0]; - if (!/^> \[!.+\]/.test(line)) return; + if (!MSDOCREGEX.test(line)) return; const params = self.getMSParametersFromLine(line); diff --git a/src/settings.ts b/src/settings.ts index 1bf4bf1..950f490 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -410,6 +410,35 @@ export default class AdmonitionSetting extends PluginSettingTab { }); }); if (this.plugin.data.allowMSSyntax) { + new Setting(containerEl) + .setName( + "Use Microsoft Document Syntax for Indented Codeblocks" + ) + .setDesc( + createFragment((e) => { + e.createSpan({ + text: "The plugin will render code blocks created by indentation using the " + }); + e.createEl("a", { + href: "https://docs.microsoft.com/en-us/contribute/markdown-reference", + text: "Microsoft Document Syntax." + }); + e.createEl("br"); + + const strong = e.createEl("strong"); + + strong.appendChild(WARNING_ICON.cloneNode(true)); + strong.createSpan({text: "This syntax will not work in Live Preview."}) + }) + ) + .addToggle((t) => { + t.setValue(this.plugin.data.msSyntaxIndented).onChange( + (v) => { + this.plugin.data.msSyntaxIndented = v; + this.plugin.saveSettings(); + } + ); + }); new Setting(containerEl) .setName("Render Microsoft Document Syntax in Live Preview") .setDesc( diff --git a/src/util/constants.ts b/src/util/constants.ts index 5e9f23c..9232ada 100644 --- a/src/util/constants.ts +++ b/src/util/constants.ts @@ -6,7 +6,7 @@ export const ADD_COMMAND_NAME = Symbol("add-command"); export const REMOVE_ADMONITION_COMMAND_ICON = ``; export const REMOVE_COMMAND_NAME = Symbol("remove-command"); -export const MSDOCREGEX = /^> \[!(\w+)(?::[ ]?(.+)?)?\](x|\+|\-)?/; +export const MSDOCREGEX = /^(?:> |\t|[ ]{4})\[!(\w+)(?::[ ]?(.+)?)?\](x|\+|\-)?/; export const ADMONITION_MAP: Record = { note: {