diff --git a/src/main.ts b/src/main.ts index 9a81660..024fad1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -106,8 +106,14 @@ export default class ObsidianAdmonition this.data.userAdmonitions && (!this.data.version || Number(this.data.version.split(".")[0]) < 5) ) { - const test = {}; for (let admonition in this.data.userAdmonitions) { + if ( + Object.prototype.hasOwnProperty.call( + this.data.userAdmonitions[admonition], + "type" + ) + ) + continue; this.data.userAdmonitions[admonition] = { ...this.data.userAdmonitions[admonition], icon: { diff --git a/src/util/util.ts b/src/util/util.ts index 3433bba..76f2a85 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -3,182 +3,187 @@ import { getIconNode } from "./icons"; import { AdmonitionIconDefinition, INestedAdmonition } from "../@types"; export function getMatches( - src: string, - from: number, - toMatch: string + src: string, + from: number, + toMatch: string ): INestedAdmonition { - const split = src.split("\n").slice(from); - const first = split.indexOf(split.find((l) => l == toMatch)); + const split = src.split("\n").slice(from); + const first = split.indexOf(split.find((l) => l == toMatch)); - let next = first + 1; - for (; next < split.length; next++) { - if (!/^(?: {2,4}|\t)+[\s\S]*?/.test(split[next])) break; - } + let next = first + 1; + for (; next < split.length; next++) { + if (!/^(?: {2,4}|\t)+[\s\S]*?/.test(split[next])) break; + } - let innerSrc = split.slice(first + 1, next).join("\n"); + let innerSrc = split.slice(first + 1, next).join("\n"); - const toRemove = innerSrc.split("\n")[0].match(/^(\s+)/); - innerSrc = innerSrc.replace(new RegExp(`^${toRemove[0] || ""}`, "gm"), ""); + const toRemove = innerSrc.split("\n")[0].match(/^(\s+)/); + innerSrc = innerSrc.replace(new RegExp(`^${toRemove[0] || ""}`, "gm"), ""); - return { - start: first + from, - end: next + from - 1, - src: innerSrc, - type: toMatch.split("-").pop(), - }; + return { + start: first + from, + end: next + from - 1, + src: innerSrc, + type: toMatch.split("-").pop() + }; } function startsWithAny(str: string, needles: string[]) { - for (let i = 0; i < needles.length; i++) { - if (str.startsWith(needles[i])) { - return i; - } - } + for (let i = 0; i < needles.length; i++) { + if (str.startsWith(needles[i])) { + return i; + } + } - return false; + return false; } export function getParametersFromSource(type: string, src: string) { - const keywordTokens = ["title:", "collapse:"]; + const keywordTokens = ["title:", "collapse:"]; - const keywords = ["title", "collapse"]; + const keywords = ["title", "collapse"]; - let lines = src.split("\n"); + let lines = src.split("\n"); - let skipLines = 0; + let skipLines = 0; - let params: { [k: string]: string } = {}; + let params: { [k: string]: string } = {}; - for (let i = 0; i < lines.length; i++) { - let keywordIndex = startsWithAny(lines[i], keywordTokens); + for (let i = 0; i < lines.length; i++) { + let keywordIndex = startsWithAny(lines[i], keywordTokens); - if (keywordIndex === false) { - break; - } + if (keywordIndex === false) { + break; + } - let foundKeyword = keywords[keywordIndex]; + let foundKeyword = keywords[keywordIndex]; - if (params[foundKeyword] !== undefined) { - break; - } + if (params[foundKeyword] !== undefined) { + break; + } - params[foundKeyword] = lines[i] - .substr(keywordTokens[keywordIndex].length) - .trim(); - ++skipLines; - } + params[foundKeyword] = lines[i] + .substr(keywordTokens[keywordIndex].length) + .trim(); + ++skipLines; + } - let { - title = type[0].toUpperCase() + type.slice(1).toLowerCase(), - collapse /* = "none" */, - } = params; + let { + title = type[0].toUpperCase() + type.slice(1).toLowerCase(), + collapse /* = "none" */ + } = params; - let content = lines.slice(skipLines).join("\n"); + let content = lines.slice(skipLines).join("\n"); - /** - * If the admonition should collapse, but something other than open or closed was provided, set to closed. - */ - if ( + /** + * If the admonition should collapse, but something other than open or closed was provided, set to closed. + */ + if ( collapse && - collapse.length && - collapse !== "none" && - collapse !== "open" && - collapse !== "closed" - ) { - collapse = "closed"; - } - - /** - * If the admonition should collapse, but title was blanked, set the default title. - */ - if (title.trim() === "" && collapse !== "none") { - title = type[0].toUpperCase() + type.slice(1).toLowerCase(); - new Notice("An admonition must have a title if it is collapsible."); - } - - return { title, collapse, content }; + collapse.length && + collapse !== "none" && + collapse !== "open" && + collapse !== "closed" + ) { + collapse = "closed"; + } + + /** + * If the admonition should collapse, but title was blanked, set the default title. + */ + if ( + title.trim() === "" && + collapse && + collapse.length && + collapse !== "none" + ) { + title = type[0].toUpperCase() + type.slice(1).toLowerCase(); + new Notice("An admonition must have a title if it is collapsible."); + } + + return { title, collapse, content }; } export /* async */ function getAdmonitionElement( - type: string, - title: string, - icon: AdmonitionIconDefinition, - color: string, - collapse?: string + type: string, + title: string, + icon: AdmonitionIconDefinition, + color: string, + collapse?: string ): HTMLElement { - let admonition, - titleEl, - attrs: { style: string; open?: string } = { - style: `--admonition-color: ${color};`, - }; - if (collapse) { - if (collapse === "open") { - attrs.open = "open"; - } - admonition = createEl("details", { - cls: `admonition admonition-${type}`, - attr: attrs, - }); - titleEl = admonition.createEl("summary", { - cls: `admonition-title ${!title.trim().length ? "no-title" : ""}`, - }); - } else { - admonition = createDiv({ - cls: `admonition admonition-${type}`, - attr: attrs, - }); - titleEl = admonition.createDiv({ - cls: `admonition-title ${!title.trim().length ? "no-title" : ""}`, - }); - } - - if (title && title.length) { - /** - * Title structure - *