Skip to content

Commit

Permalink
feat!: Removes support for Python-style non-codeblock Admonitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Feb 25, 2022
1 parent 2f87101 commit b6ad0c4
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 233 deletions.
1 change: 0 additions & 1 deletion src/@types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export interface AdmonitionSettings {
syncLinks: boolean;
version: string;
warnedAboutNC: boolean;
enableMarkdownProcessor: boolean;
injectColor: boolean;
parseTitles: boolean;
allowMSSyntax: boolean;
Expand Down
197 changes: 0 additions & 197 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ const DEFAULT_APP_SETTINGS: AdmonitionSettings = {
autoCollapse: false,
defaultCollapseType: "open",
syncLinks: true,
enableMarkdownProcessor: false,
injectColor: true,
parseTitles: true,
allowMSSyntax: true,
Expand Down Expand Up @@ -266,10 +265,6 @@ export default class ObsidianAdmonition extends Plugin {
REMOVE_ADMONITION_COMMAND_ICON
);

if (this.data.enableMarkdownProcessor) {
this.enableMarkdownProcessor();
}

if (this.data.syntaxHighlight) {
this.turnOnSyntaxHighlighting();
}
Expand Down Expand Up @@ -711,171 +706,6 @@ export default class ObsidianAdmonition extends Plugin {

this.registerEditorExtension([plugin, field]);
}
enableMarkdownProcessor() {
if (!this.data.enableMarkdownProcessor) return;
const TYPE_REGEX = new RegExp(
`(!{3,}|\\?{3,}\\+?) ad-(${this.types.join("|")})(\\s[\\s\\S]+)?`
);
const END_REGEX = new RegExp(`\\-{3,} admonition`);

let push = false,
id: string;
const childMap: Map<
MarkdownRenderChild,
{ contentEl: HTMLElement; elements: Element[]; loaded: boolean }
> = new Map();
const elementMap: Map<Element, MarkdownRenderChild> = new Map();
const idMap: Map<string, MarkdownRenderChild> = new Map();

Object.values(this.admonitions)
.filter(({ command }) => command)
.forEach((admonition) => {
this.registerCommandsFor(admonition);
});

this.registerMarkdownPostProcessor(async (el, ctx) => {
if (!this.data.enableMarkdownProcessor) return;

if (END_REGEX.test(el.textContent) && push) {
push = false;
const lastElement = createDiv();
if (
id &&
idMap.has(id) &&
childMap.has(idMap.get(id)) &&
el.children[0].textContent.replace(END_REGEX, "").length
) {
lastElement.innerHTML = el.children[0].outerHTML.replace(
new RegExp(`(<br>)?\\n?${END_REGEX.source}`),
""
);
const contentEl = childMap.get(idMap.get(id)).contentEl;
if (contentEl)
contentEl.appendChild(lastElement.children[0]);
}

el.children[0].detach();
return;
}

if (!TYPE_REGEX.test(el.textContent) && !push) return;
if (!push) {
if (
!(
Array.from(el.children).find((e) =>
TYPE_REGEX.test(e.textContent)
) instanceof HTMLParagraphElement
)
)
return;
push = true;
let child = new MarkdownRenderChild(el);
id = getID();
idMap.set(id, child);

childMap.set(child, {
contentEl: null,
elements: [],
loaded: false
});

child.onload = async () => {
const source = el.textContent;

let [
,
col,
type,
title = type[0].toUpperCase() +
type.slice(1).toLowerCase()
]: string[] = source.match(TYPE_REGEX) ?? [];

if (!type) return;
let collapse;
if (/\?{3,}/.test(col)) {
collapse = /\+/.test(col) ? "open" : "closed";
}

if (
(title.trim() === "" || title === '""') &&
collapse !== undefined &&
collapse !== "none"
) {
title =
type[0].toUpperCase() + type.slice(1).toLowerCase();
new Notice(
"An admonition must have a title if it is collapsible."
);
}
const admonition = this.admonitions[type];
const admonitionElement =
await this.getAdmonitionElementAsync(
type,
title.trim(),
admonition.icon,
admonition.injectColor ?? this.data.injectColor
? admonition.color
: null,
collapse
);

const contentHolder = admonitionElement.createDiv(
"admonition-content-holder"
);

const contentEl =
contentHolder.createDiv("admonition-content");

child.containerEl.appendChild(admonitionElement);
for (let element of childMap.get(child)?.elements) {
contentEl.appendChild(element);
}

childMap.set(child, {
...childMap.get(child),
contentEl: contentEl,
loaded: true
});
};

child.onunload = () => {
idMap.delete(id);
childMap.delete(child);
};

ctx.addChild(child);

el.children[0].detach();

return;
}

if (id && idMap.get(id)) {
const child = idMap.get(id);
childMap.set(child, {
...childMap.get(child),
elements: [
...childMap.get(child).elements,
...Array.from(el.children)
]
});
elementMap.set(el, child);
if (childMap.get(child)?.loaded) {
for (let element of childMap.get(child)?.elements) {
childMap.get(child).contentEl.appendChild(element);
}
}
}
});
}
disableMarkdownProcessor() {
/* new Notice("The plugin must be reloaded for this to take effect."); */
Object.values(this.admonitions)
.filter(({ command }) => command)
.forEach((admonition) => {
this.registerCommandsFor(admonition);
});
}
unregisterCommandsFor(admonition: Admonition) {
admonition.command = false;

Expand Down Expand Up @@ -950,33 +780,6 @@ ${editor.getDoc().getSelection()}
}
}
});
if (this.data.enableMarkdownProcessor) {
this.addCommand({
id: `insert-non-${admonition.type}`,
name: `Insert Non-codeblock ${admonition.type}`,
editorCheckCallback: (checking, editor, view) => {
if (checking)
return (
admonition.command &&
this.data.enableMarkdownProcessor
);
if (admonition.command) {
try {
editor.getDoc().replaceSelection(
`!!! ad-${admonition.type}\n
${editor.getDoc().getSelection()}\n--- admonition\n`
);
const cursor = editor.getCursor();
editor.setCursor(cursor.line - 2);
} catch (e) {
new Notice(
"There was an issue inserting the admonition."
);
}
}
}
});
}
}
turnOnSyntaxHighlighting(types: string[] = Object.keys(this.admonitions)) {
if (!this.data.syntaxHighlight) return;
Expand Down
35 changes: 0 additions & 35 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,41 +354,6 @@ export default class AdmonitionSetting extends PluginSettingTab {
new Setting(summary).setHeading().setName("Additional Syntaxes");
summary.createDiv("collapser").createDiv("handle");

new Setting(containerEl)
.setName(
createFragment((e) => {
e.appendChild(WARNING_ICON.cloneNode(true));
e.createSpan({
text: t(" Enable Non-codeblock Admonitions")
});
})
)
.setDesc(
createFragment((e) => {
e.createSpan({
text: t("Allows admonitions to be created using ")
});
e.createEl("code", { text: "!!! ad-<type>" });
e.createSpan({
text: t(" or ")
});
e.createEl("code", { text: "??? ad-<type>" });
e.createSpan({
text: t(", instead of using a code block.")
});
e.createEl("br");
e.appendChild(WARNING_ICON.cloneNode(true));
e.createEl("strong", {
text: " Legacy support only. This setting will be removed in a future version. Use the MSDoc-syntax instead."
});
})
)
.addToggle((t) => {
t.setValue(
this.plugin.data.enableMarkdownProcessor
).setDisabled(true);
});

new Setting(containerEl)
.setName("Allow Microsoft Document Syntax")
.setDesc(
Expand Down

0 comments on commit b6ad0c4

Please sign in to comment.