Skip to content

Commit

Permalink
Merge pull request #43 from valentine195:valentine195/issue42
Browse files Browse the repository at this point in the history
5.0.2
  • Loading branch information
valentine195 authored Jun 7, 2021
2 parents 79030e8 + 9742ef3 commit a5df399
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 150 deletions.
8 changes: 7 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
303 changes: 154 additions & 149 deletions src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <div|summary>.admonition-title
* <element>.admonition-title-content - Rendered Markdown top-level element (e.g. H1/2/3 etc, p)
* div.admonition-title-icon
* svg
* div.admonition-title-markdown - Container of rendered markdown
* ...rendered markdown children...
*/

//get markdown
const markdownHolder = createDiv();
MarkdownRenderer.renderMarkdown(title, markdownHolder, "", null);

//admonition-title-content is first child of rendered markdown
const admonitionTitleContent = markdownHolder.children[0];

//get children of markdown element, then remove them
const markdownElements = Array.from(
admonitionTitleContent?.childNodes || []
);
admonitionTitleContent.innerHTML = "";
admonitionTitleContent.addClass("admonition-title-content");

//build icon element
const iconEl = admonitionTitleContent.createDiv(
"admonition-title-icon"
);
if (icon && icon.name && icon.type) {
iconEl.appendChild(getIconNode(icon));
}

//add markdown children back
const admonitionTitleMarkdown = admonitionTitleContent.createDiv(
"admonition-title-markdown"
);
for (let i = 0; i < markdownElements.length; i++) {
admonitionTitleMarkdown.appendChild(markdownElements[i]);
}
titleEl.appendChild(admonitionTitleContent || createDiv());
}

//add them to title element

if (collapse) {
titleEl.createDiv("collapser").createDiv("handle");
}
return admonition;
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
* <div|summary>.admonition-title
* <element>.admonition-title-content - Rendered Markdown top-level element (e.g. H1/2/3 etc, p)
* div.admonition-title-icon
* svg
* div.admonition-title-markdown - Container of rendered markdown
* ...rendered markdown children...
*/

//get markdown
const markdownHolder = createDiv();
MarkdownRenderer.renderMarkdown(title, markdownHolder, "", null);

//admonition-title-content is first child of rendered markdown
const admonitionTitleContent = markdownHolder.children[0];

//get children of markdown element, then remove them
const markdownElements = Array.from(
admonitionTitleContent?.childNodes || []
);
admonitionTitleContent.innerHTML = "";
admonitionTitleContent.addClass("admonition-title-content");

//build icon element
const iconEl = admonitionTitleContent.createDiv(
"admonition-title-icon"
);
if (icon && icon.name && icon.type) {
iconEl.appendChild(getIconNode(icon));
}

//add markdown children back
const admonitionTitleMarkdown = admonitionTitleContent.createDiv(
"admonition-title-markdown"
);
for (let i = 0; i < markdownElements.length; i++) {
admonitionTitleMarkdown.appendChild(markdownElements[i]);
}
titleEl.appendChild(admonitionTitleContent || createDiv());
}

//add them to title element

if (collapse) {
titleEl.createDiv("collapser").createDiv("handle");
}
return admonition;
}

0 comments on commit a5df399

Please sign in to comment.