Skip to content

Commit e074cbd

Browse files
authored
Merge pull request #39 from mitasov-ra/patch-1
Updated getParametersFromSource()
2 parents 7c90449 + c0a8f3d commit e074cbd

File tree

1 file changed

+50
-28
lines changed

1 file changed

+50
-28
lines changed

src/util.ts

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,51 +29,73 @@ export function getMatches(
2929
};
3030
}
3131

32+
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+
}
38+
39+
return false;
40+
}
41+
3242
export function getParametersFromSource(type: string, src: string) {
33-
/**
34-
* Find title and collapse parameters.
35-
*/
36-
let matchedParameters =
37-
src.match(/^\b(title|collapse)\b:([\s\S]*?)$/gm) || [];
3843

39-
let params = Object.fromEntries(
40-
matchedParameters.map((p) => {
41-
let [, param, rest] = p.match(/^\b(title|collapse)\b:([\s\S]*?)$/);
42-
return [param.trim(), rest.trim()];
43-
})
44-
);
44+
const keywordTokens = [
45+
'title:',
46+
'collapse:',
47+
]
48+
49+
const keywords = [
50+
'title',
51+
'collapse',
52+
]
53+
54+
let lines = src.split("\n");
55+
56+
let skipLines = 0
57+
58+
let params: {[k: string]: string} = {}
59+
60+
for (let i = 0; i < lines.length; i++) {
61+
let keywordIndex = startsWithAny(lines[i], keywordTokens)
62+
63+
if (keywordIndex === false) {
64+
break
65+
}
66+
67+
let foundKeyword = keywords[keywordIndex]
68+
69+
if (params[foundKeyword] !== undefined) {
70+
break
71+
}
72+
73+
params[foundKeyword] = lines[i].substr(keywordTokens[keywordIndex].length).trim()
74+
++skipLines
75+
}
4576

4677
let {
4778
title = type[0].toUpperCase() + type.slice(1).toLowerCase(),
48-
collapse
79+
collapse = "none"
4980
} = params;
5081

51-
/**
52-
* Get the content. Content should be everything that is not the title or collapse parameters.
53-
* Remove any "content: " fields (legacy from < v0.2.0)
54-
*/
55-
let content = src
56-
.replace(/^\b(title|collapse)\b:([\s\S]*?)$/gm, "")
57-
.replace(/^\bcontent\b:\s?/gm, "");
82+
let content = lines.slice(skipLines).join("\n")
83+
5884
/**
5985
* If the admonition should collapse, but something other than open or closed was provided, set to closed.
6086
*/
6187
if (
62-
Object.prototype.hasOwnProperty.call(params, "collapse") &&
63-
(params.collapse.length == 0 ||
64-
params.collapse === undefined ||
65-
(collapse !== "open" && collapse !== "none"))
88+
collapse !== "none" &&
89+
collapse !== "open" &&
90+
collapse !== "closed"
6691
) {
6792
collapse = "closed";
6893
}
94+
6995
/**
7096
* If the admonition should collapse, but title was blanked, set the default title.
7197
*/
72-
if (
73-
Object.prototype.hasOwnProperty.call(params, "title") &&
74-
(params.title === undefined || params.title.length === 0) &&
75-
collapse
76-
) {
98+
if (title.trim() === "" && collapse !== "none") {
7799
title = type[0].toUpperCase() + type.slice(1).toLowerCase();
78100
new Notice("An admonition must have a title if it is collapsible.");
79101
}

0 commit comments

Comments
 (0)