Skip to content

Commit

Permalink
feat: adds support for indented code blocks using MSDoc syntax (close #…
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Feb 18, 2022
1 parent 903f18a commit 9c9e6ac
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -637,6 +646,10 @@ Enabled use of `!!! ad-<type>` 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.
Expand Down
1 change: 1 addition & 0 deletions src/@types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface AdmonitionSettings {
injectColor: boolean;
parseTitles: boolean;
allowMSSyntax: boolean;
msSyntaxIndented: boolean;
livePreviewMS: boolean;
dropShadow: boolean;
hideEmpty: boolean;
Expand Down
17 changes: 13 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ const DEFAULT_APP_SETTINGS: AdmonitionSettings = {
injectColor: true,
parseTitles: true,
allowMSSyntax: true,
msSyntaxIndented: true,
livePreviewMS: true,
dropShadow: true,
hideEmpty: false
Expand Down Expand Up @@ -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);

Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
29 changes: 29 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/util/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const ADD_COMMAND_NAME = Symbol("add-command");
export const REMOVE_ADMONITION_COMMAND_ICON = `<svg viewBox="0 0 100 100" class="remove-admonition-command"><path fill="currentColor" stroke="currentColor" d="M37,16c-4.4,0-8.3,3.3-9.2,7.6l-11.6,52c-0.5,2.2,0,4.3,1.2,5.9c1.2,1.6,3.2,2.6,5.4,2.6H79c4.4,0,8.3-3.3,9.2-7.6 l11.6-52c0.5-2.2,0-4.3-1.2-5.9C97.4,17,95.4,16,93.2,16L37,16z M37,20h56.2c1.1,0,1.8,0.4,2.2,1c0.5,0.6,0.7,1.4,0.4,2.6l-1,4.4 H30.8l0.8-3.6C32.1,22.2,34.8,20,37,20z M29.9,32H94l-9.6,43.6C83.9,77.8,81.2,80,79,80H22.8c-1.1,0-1.8-0.4-2.2-1 c-0.5-0.6-0.7-1.4-0.4-2.6L29.9,32z M0,36v4h19.6l0.9-4L0,36z M36.7,38c-0.8,0.1-1.4,0.7-1.6,1.5l-3.5,14c-0.2,0.6,0,1.2,0.4,1.7 c0.4,0.5,1,0.8,1.6,0.8H81c0.9,0,1.7-0.6,1.9-1.5l3.5-14c0.2-0.6,0-1.3-0.4-1.8c-0.4-0.5-1-0.8-1.6-0.8H37.1c-0.1,0-0.1,0-0.2,0 C36.9,38,36.8,38,36.7,38L36.7,38z M38.7,42h43.2l-2.4,10H36.2L38.7,42z M0,52v4h16l0.9-4H0z M0,68v4h12.4l0.9-4H0z"></path><circle fill="white" transform="translate(50 50) scale(3 3)" cx="8" cy="8" r="8"/><path fill="#dc3545" transform="translate(50 50) scale(3 3)" d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM4.5 7.5a.5.5 0 0 0 0 1h7a.5.5 0 0 0 0-1h-7z"/></svg>`;
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<string, Admonition> = {
note: {
Expand Down

0 comments on commit 9c9e6ac

Please sign in to comment.