|
1 | 1 | import { MarkdownRenderer, Notice } from "obsidian"; |
2 | | -import { findIconDefinition, /* icon, */ IconName } from "./icons"; |
3 | 2 | import { getIconNode } from "./icons"; |
4 | 3 | import { AdmonitionIconDefinition, INestedAdmonition } from "../@types"; |
5 | 4 |
|
6 | 5 | export function getMatches( |
7 | | - src: string, |
8 | | - from: number, |
9 | | - toMatch: string |
| 6 | + src: string, |
| 7 | + from: number, |
| 8 | + toMatch: string |
10 | 9 | ): INestedAdmonition { |
11 | | - const split = src.split("\n").slice(from); |
12 | | - const first = split.indexOf(split.find((l) => l == toMatch)); |
| 10 | + const split = src.split("\n").slice(from); |
| 11 | + const first = split.indexOf(split.find((l) => l == toMatch)); |
13 | 12 |
|
14 | | - let next = first + 1; |
15 | | - for (; next < split.length; next++) { |
16 | | - if (!/^(?: {2,4}|\t)+[\s\S]*?/.test(split[next])) break; |
17 | | - } |
| 13 | + let next = first + 1; |
| 14 | + for (; next < split.length; next++) { |
| 15 | + if (!/^(?: {2,4}|\t)+[\s\S]*?/.test(split[next])) break; |
| 16 | + } |
18 | 17 |
|
19 | | - let innerSrc = split.slice(first + 1, next).join("\n"); |
| 18 | + let innerSrc = split.slice(first + 1, next).join("\n"); |
20 | 19 |
|
21 | | - const toRemove = innerSrc.split("\n")[0].match(/^(\s+)/); |
22 | | - innerSrc = innerSrc.replace(new RegExp(`^${toRemove[0] || ""}`, "gm"), ""); |
| 20 | + const toRemove = innerSrc.split("\n")[0].match(/^(\s+)/); |
| 21 | + innerSrc = innerSrc.replace(new RegExp(`^${toRemove[0] || ""}`, "gm"), ""); |
23 | 22 |
|
24 | | - return { |
25 | | - start: first + from, |
26 | | - end: next + from - 1, |
27 | | - src: innerSrc, |
28 | | - type: toMatch.split("-").pop() |
29 | | - }; |
| 23 | + return { |
| 24 | + start: first + from, |
| 25 | + end: next + from - 1, |
| 26 | + src: innerSrc, |
| 27 | + type: toMatch.split("-").pop(), |
| 28 | + }; |
30 | 29 | } |
31 | 30 |
|
32 | 31 | function startsWithAny(str: string, needles: string[]) { |
33 | | - for (let i = 0; i < needles.length; i++) { |
34 | | - if (str.startsWith(needles[i])) { |
35 | | - return i; |
36 | | - } |
37 | | - } |
| 32 | + for (let i = 0; i < needles.length; i++) { |
| 33 | + if (str.startsWith(needles[i])) { |
| 34 | + return i; |
| 35 | + } |
| 36 | + } |
38 | 37 |
|
39 | | - return false; |
| 38 | + return false; |
40 | 39 | } |
41 | 40 |
|
42 | 41 | export function getParametersFromSource(type: string, src: string) { |
43 | | - const keywordTokens = ["title:", "collapse:"]; |
| 42 | + const keywordTokens = ["title:", "collapse:"]; |
44 | 43 |
|
45 | | - const keywords = ["title", "collapse"]; |
| 44 | + const keywords = ["title", "collapse"]; |
46 | 45 |
|
47 | | - let lines = src.split("\n"); |
| 46 | + let lines = src.split("\n"); |
48 | 47 |
|
49 | | - let skipLines = 0; |
| 48 | + let skipLines = 0; |
50 | 49 |
|
51 | | - let params: { [k: string]: string } = {}; |
| 50 | + let params: { [k: string]: string } = {}; |
52 | 51 |
|
53 | | - for (let i = 0; i < lines.length; i++) { |
54 | | - let keywordIndex = startsWithAny(lines[i], keywordTokens); |
| 52 | + for (let i = 0; i < lines.length; i++) { |
| 53 | + let keywordIndex = startsWithAny(lines[i], keywordTokens); |
55 | 54 |
|
56 | | - if (keywordIndex === false) { |
57 | | - break; |
58 | | - } |
| 55 | + if (keywordIndex === false) { |
| 56 | + break; |
| 57 | + } |
59 | 58 |
|
60 | | - let foundKeyword = keywords[keywordIndex]; |
| 59 | + let foundKeyword = keywords[keywordIndex]; |
61 | 60 |
|
62 | | - if (params[foundKeyword] !== undefined) { |
63 | | - break; |
64 | | - } |
| 61 | + if (params[foundKeyword] !== undefined) { |
| 62 | + break; |
| 63 | + } |
65 | 64 |
|
66 | | - params[foundKeyword] = lines[i] |
67 | | - .substr(keywordTokens[keywordIndex].length) |
68 | | - .trim(); |
69 | | - ++skipLines; |
70 | | - } |
| 65 | + params[foundKeyword] = lines[i] |
| 66 | + .substr(keywordTokens[keywordIndex].length) |
| 67 | + .trim(); |
| 68 | + ++skipLines; |
| 69 | + } |
71 | 70 |
|
72 | | - let { |
73 | | - title = type[0].toUpperCase() + type.slice(1).toLowerCase(), |
74 | | - collapse = "none" |
75 | | - } = params; |
| 71 | + let { |
| 72 | + title = type[0].toUpperCase() + type.slice(1).toLowerCase(), |
| 73 | + collapse /* = "none" */, |
| 74 | + } = params; |
76 | 75 |
|
77 | | - let content = lines.slice(skipLines).join("\n"); |
| 76 | + let content = lines.slice(skipLines).join("\n"); |
78 | 77 |
|
79 | | - /** |
80 | | - * If the admonition should collapse, but something other than open or closed was provided, set to closed. |
81 | | - */ |
82 | | - if (collapse !== "none" && collapse !== "open" && collapse !== "closed") { |
83 | | - collapse = "closed"; |
84 | | - } |
| 78 | + /** |
| 79 | + * If the admonition should collapse, but something other than open or closed was provided, set to closed. |
| 80 | + */ |
| 81 | + if ( |
| 82 | + collapse && |
| 83 | + collapse.length && |
| 84 | + collapse !== "none" && |
| 85 | + collapse !== "open" && |
| 86 | + collapse !== "closed" |
| 87 | + ) { |
| 88 | + collapse = "closed"; |
| 89 | + } |
85 | 90 |
|
86 | | - /** |
87 | | - * If the admonition should collapse, but title was blanked, set the default title. |
88 | | - */ |
89 | | - if (title.trim() === "" && collapse !== "none") { |
90 | | - title = type[0].toUpperCase() + type.slice(1).toLowerCase(); |
91 | | - new Notice("An admonition must have a title if it is collapsible."); |
92 | | - } |
| 91 | + /** |
| 92 | + * If the admonition should collapse, but title was blanked, set the default title. |
| 93 | + */ |
| 94 | + if (title.trim() === "" && collapse !== "none") { |
| 95 | + title = type[0].toUpperCase() + type.slice(1).toLowerCase(); |
| 96 | + new Notice("An admonition must have a title if it is collapsible."); |
| 97 | + } |
93 | 98 |
|
94 | | - return { title, collapse, content }; |
| 99 | + return { title, collapse, content }; |
95 | 100 | } |
96 | 101 |
|
97 | 102 | export /* async */ function getAdmonitionElement( |
98 | | - type: string, |
99 | | - title: string, |
100 | | - icon: AdmonitionIconDefinition, |
101 | | - color: string, |
102 | | - collapse?: string |
| 103 | + type: string, |
| 104 | + title: string, |
| 105 | + icon: AdmonitionIconDefinition, |
| 106 | + color: string, |
| 107 | + collapse?: string |
103 | 108 | ): HTMLElement { |
104 | | - let admonition, |
105 | | - titleEl, |
106 | | - attrs: { style: string; open?: string } = { |
107 | | - style: `--admonition-color: ${color};` |
108 | | - }; |
109 | | - if (collapse) { |
110 | | - if (collapse === "open") { |
111 | | - attrs.open = "open"; |
112 | | - } |
113 | | - admonition = createEl("details", { |
114 | | - cls: `admonition admonition-${type}`, |
115 | | - attr: attrs |
116 | | - }); |
117 | | - titleEl = admonition.createEl("summary", { |
118 | | - cls: `admonition-title ${!title.trim().length ? "no-title" : ""}` |
119 | | - }); |
120 | | - } else { |
121 | | - admonition = createDiv({ |
122 | | - cls: `admonition admonition-${type}`, |
123 | | - attr: attrs |
124 | | - }); |
125 | | - titleEl = admonition.createDiv({ |
126 | | - cls: `admonition-title ${!title.trim().length ? "no-title" : ""}` |
127 | | - }); |
128 | | - } |
129 | | - |
130 | | - if (title && title.length) { |
131 | | - /** |
132 | | - * Title structure |
133 | | - * <div|summary>.admonition-title |
134 | | - * <element>.admonition-title-content - Rendered Markdown top-level element (e.g. H1/2/3 etc, p) |
135 | | - * div.admonition-title-icon |
136 | | - * svg |
137 | | - * div.admonition-title-markdown - Container of rendered markdown |
138 | | - * ...rendered markdown children... |
139 | | - */ |
140 | | - |
141 | | - //get markdown |
142 | | - const markdownHolder = createDiv(); |
143 | | - MarkdownRenderer.renderMarkdown(title, markdownHolder, "", null); |
144 | | - |
145 | | - //admonition-title-content is first child of rendered markdown |
146 | | - const admonitionTitleContent = markdownHolder.children[0]; |
147 | | - |
148 | | - //get children of markdown element, then remove them |
149 | | - const markdownElements = Array.from( |
150 | | - admonitionTitleContent?.childNodes || [] |
151 | | - ); |
152 | | - admonitionTitleContent.innerHTML = ""; |
153 | | - admonitionTitleContent.addClass("admonition-title-content"); |
154 | | - |
155 | | - //build icon element |
156 | | - const iconEl = admonitionTitleContent.createDiv( |
157 | | - "admonition-title-icon" |
158 | | - ); |
159 | | - if (icon && icon.name && icon.type) { |
160 | | - iconEl.appendChild(getIconNode(icon)); |
161 | | - } |
162 | | - |
163 | | - //add markdown children back |
164 | | - const admonitionTitleMarkdown = admonitionTitleContent.createDiv( |
165 | | - "admonition-title-markdown" |
166 | | - ); |
167 | | - for (let i = 0; i < markdownElements.length; i++) { |
168 | | - admonitionTitleMarkdown.appendChild(markdownElements[i]); |
169 | | - } |
170 | | - titleEl.appendChild(admonitionTitleContent || createDiv()); |
171 | | - } |
172 | | - |
173 | | - //add them to title element |
174 | | - |
175 | | - if (collapse) { |
176 | | - titleEl.createDiv("collapser").createDiv("handle"); |
177 | | - } |
178 | | - return admonition; |
| 109 | + let admonition, |
| 110 | + titleEl, |
| 111 | + attrs: { style: string; open?: string } = { |
| 112 | + style: `--admonition-color: ${color};`, |
| 113 | + }; |
| 114 | + if (collapse) { |
| 115 | + if (collapse === "open") { |
| 116 | + attrs.open = "open"; |
| 117 | + } |
| 118 | + admonition = createEl("details", { |
| 119 | + cls: `admonition admonition-${type}`, |
| 120 | + attr: attrs, |
| 121 | + }); |
| 122 | + titleEl = admonition.createEl("summary", { |
| 123 | + cls: `admonition-title ${!title.trim().length ? "no-title" : ""}`, |
| 124 | + }); |
| 125 | + } else { |
| 126 | + admonition = createDiv({ |
| 127 | + cls: `admonition admonition-${type}`, |
| 128 | + attr: attrs, |
| 129 | + }); |
| 130 | + titleEl = admonition.createDiv({ |
| 131 | + cls: `admonition-title ${!title.trim().length ? "no-title" : ""}`, |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + if (title && title.length) { |
| 136 | + /** |
| 137 | + * Title structure |
| 138 | + * <div|summary>.admonition-title |
| 139 | + * <element>.admonition-title-content - Rendered Markdown top-level element (e.g. H1/2/3 etc, p) |
| 140 | + * div.admonition-title-icon |
| 141 | + * svg |
| 142 | + * div.admonition-title-markdown - Container of rendered markdown |
| 143 | + * ...rendered markdown children... |
| 144 | + */ |
| 145 | + |
| 146 | + //get markdown |
| 147 | + const markdownHolder = createDiv(); |
| 148 | + MarkdownRenderer.renderMarkdown(title, markdownHolder, "", null); |
| 149 | + |
| 150 | + //admonition-title-content is first child of rendered markdown |
| 151 | + const admonitionTitleContent = markdownHolder.children[0]; |
| 152 | + |
| 153 | + //get children of markdown element, then remove them |
| 154 | + const markdownElements = Array.from( |
| 155 | + admonitionTitleContent?.childNodes || [] |
| 156 | + ); |
| 157 | + admonitionTitleContent.innerHTML = ""; |
| 158 | + admonitionTitleContent.addClass("admonition-title-content"); |
| 159 | + |
| 160 | + //build icon element |
| 161 | + const iconEl = admonitionTitleContent.createDiv( |
| 162 | + "admonition-title-icon" |
| 163 | + ); |
| 164 | + if (icon && icon.name && icon.type) { |
| 165 | + iconEl.appendChild(getIconNode(icon)); |
| 166 | + } |
| 167 | + |
| 168 | + //add markdown children back |
| 169 | + const admonitionTitleMarkdown = admonitionTitleContent.createDiv( |
| 170 | + "admonition-title-markdown" |
| 171 | + ); |
| 172 | + for (let i = 0; i < markdownElements.length; i++) { |
| 173 | + admonitionTitleMarkdown.appendChild(markdownElements[i]); |
| 174 | + } |
| 175 | + titleEl.appendChild(admonitionTitleContent || createDiv()); |
| 176 | + } |
| 177 | + |
| 178 | + //add them to title element |
| 179 | + |
| 180 | + if (collapse) { |
| 181 | + titleEl.createDiv("collapser").createDiv("handle"); |
| 182 | + } |
| 183 | + return admonition; |
179 | 184 | } |
0 commit comments