Skip to content

Commit

Permalink
Merge pull request #147 from valentine195:microsoft-syntax
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
valentine195 authored Jan 9, 2022
2 parents ebc55b1 + 3af3922 commit 9995b7d
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 2 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ Mermaid graphs are supported by Admonitions, but with some caveats:

## Non-code block Admonitions

Please note: An additional non-code block syntax can be seen in the [Microsoft Document Syntax](#microsoft-document-syntax) section.

As of version 6.0.0, there is a new setting: Enable Non-codeblock Admonitions.

This setting is highly experimental and may not work as expected, and there are a few caveats listed at the end of this section to keep in mind.
Expand Down Expand Up @@ -511,6 +513,38 @@ content

If you experience any bugs using this setting, please create an issue and I will look into them.

## Microsoft Document Syntax

As of v6.8.0, an additional non-code block syntax can be used that is similar to the [Microsoft Document Syntax](https://docs.microsoft.com/en-us/contribute/markdown-reference) to render admonitions.

> Please note: The type must be the same case as defined in settings.
```md
> [!quote]
> This is an admonition!
```

![](https://raw.githubusercontent.com/valentine195/obsidian-admonition/master/images/msdocs.png)

### Title

A title can be added to the MSDoc-style admonition by appending it after the type.

```md
> [!quote: This is the title!]
> This is an admonition!
```

### Collapse

Collapse can be set by appending the following characters after the brackets:

| Character | Collapse Type |
| --------- | ------------- |
| `+` | `open` |
| `-` | `closed` |
| `x` | `none` |

## Publish

Obsidian plugins do not work on publish sites; however, version 6.4.0+ has an option to generate a JavaScript file that can be used on Publish sites with **custom domains**.
Expand Down
Binary file added images/msdocs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/@types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface ISettingsData {
enableMarkdownProcessor: boolean;
injectColor: boolean;
parseTitles: boolean;
allowMSSyntax: boolean;
}

export type AdmonitionIconDefinition = {
Expand Down
68 changes: 67 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ const DEFAULT_APP_SETTINGS: ISettingsData = {
syncLinks: true,
enableMarkdownProcessor: false,
injectColor: true,
parseTitles: true
parseTitles: true,
allowMSSyntax: true
};

export default class ObsidianAdmonition
Expand Down Expand Up @@ -323,6 +324,71 @@ export default class ObsidianAdmonition
this.addLinksToCache(admonitionLinks, file.path);
})
);

this.enableMSSyntax();
}
enableMSSyntax() {
this.registerMarkdownPostProcessor((el, ctx) => {
if (!this.data.allowMSSyntax) return;
if (el.firstChild.nodeName !== "BLOCKQUOTE") return;

const section = ctx.getSectionInfo(el);
if (!section) return;
const text = section.text.split("\n");
const firstLine = text[section.lineStart];
if (!/^> \[!.+\]/.test(firstLine)) return;

const [, type, title, col] =
firstLine.match(/^> \[!(\w+)(?:: (.+))?\](x|\+|\-)?/) ?? [];

if (!type || !this.admonitions[type]) return;

let collapse;
switch (col) {
case "+": {
collapse = "open";
break;
}
case "-": {
collapse = "closed";
break;
}
case "x": {
break;
}
default: {
collapse = this.data.autoCollapse
? this.data.defaultCollapseType
: null;
}
}

const admonition = this.getAdmonitionElement(
type,
title ??
this.admonitions[type].title ??
`${type[0].toUpperCase()}${type.slice(1).toLowerCase()}`,
this.admonitions[type].icon,
this.admonitions[type].color,
collapse
);

const content = text
.slice(section.lineStart + 1, section.lineEnd + 1)
.join("\n")
.replace(/> /g, "");

MarkdownRenderer.renderMarkdown(
content,
admonition
.createDiv("admonition-content-holder")
.createDiv("admonition-content"),
ctx.sourcePath,
null
);

el.firstElementChild.replaceWith(admonition);
});
}
enableMarkdownProcessor() {
if (!this.data.enableMarkdownProcessor) return;
Expand Down
20 changes: 20 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ export default class AdmonitionSetting extends PluginSettingTab {
createSpan({ text: t(" Enable Non-codeblock Admonitions") })
);

new Setting(containerEl)
.setName("Allow Microsoft Document Syntax")
.setDesc(
createFragment((e) => {
e.createSpan({
text: "The plugin will render blockquotes created using the "
});
e.createEl("a", {
href: "https://docs.microsoft.com/en-us/contribute/markdown-reference",
text: "Microsoft Document Syntax."
});
})
)
.addToggle((t) => {
t.setValue(this.plugin.data.allowMSSyntax).onChange((v) => {
this.plugin.data.allowMSSyntax = v;
this.plugin.saveSettings();
});
});

const publish = new Setting(containerEl)
.setName("Generate JS for Publish")
.setDesc(
Expand Down
19 changes: 18 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ module.exports = {
},
externals: {
electron: "commonjs2 electron",
obsidian: "commonjs2 obsidian"
obsidian: "commonjs2 obsidian",

codemirror: "codemirror",
"@codemirror/autocomplete": "@codemirror/autocomplete",
"@codemirror/closebrackets": "@codemirror/closebrackets",
"@codemirror/commands": "@codemirror/commands",
"@codemirror/fold": "@codemirror/fold",
"@codemirror/gutter": "@codemirror/gutter",
"@codemirror/history": "@codemirror/history",
"@codemirror/language": "@codemirror/language",
"@codemirror/rangeset": "@codemirror/rangeset",
"@codemirror/rectangular-selection":
"@codemirror/rectangular-selection",
"@codemirror/search": "@codemirror/search",
"@codemirror/state": "@codemirror/state",
"@codemirror/stream-parser": "@codemirror/stream-parser",
"@codemirror/text": "@codemirror/text",
"@codemirror/view": "@codemirror/view"
}
};

0 comments on commit 9995b7d

Please sign in to comment.