Skip to content

Commit

Permalink
fix: fixes bug where you couldn't have an empty title for the MSDoc s…
Browse files Browse the repository at this point in the history
…yntax (close #183)
  • Loading branch information
valentine195 committed Feb 17, 2022
1 parent 36af1dc commit ca3de23
Showing 1 changed file with 64 additions and 64 deletions.
128 changes: 64 additions & 64 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,57 @@ export default class ObsidianAdmonition extends Plugin {
this.enableMSSyntax();
});
}
getMSParametersFromLine(line: string) {
let [, type, title, col] = line.match(MSDOCREGEX) ?? [];

if (
!type ||
(!this.admonitions[type] &&
!Object.keys(this.admonitions)
.map((k) => k.toLowerCase())
.includes(type.toLowerCase()))
)
return;

if (!(type in this.admonitions)) {
type = Object.keys(this.admonitions).find(
(k) => k.toLowerCase() == type.toLowerCase()
);
}
if (!type) return;

if (title == undefined && !line.match(/^> \[!(\w+):[ ]?/)) {
title =
this.admonitions[type].title ??
`${type[0].toUpperCase()}${type.slice(1).toLowerCase()}`;
}

let collapse;
switch (col) {
case "+": {
collapse = "open";
break;
}
case "-": {
collapse = "closed";
break;
}
case "x": {
break;
}
default: {
collapse = this.data.autoCollapse
? this.data.defaultCollapseType
: null;
}
}
if ((collapse == "closed" || collapse == "open") && !title) {
title =
this.admonitions[type].title ??
`${type[0].toUpperCase()}${type.slice(1).toLowerCase()}`;
}
return { type, title, collapse };
}
enableMSSyntax() {
this.registerMarkdownPostProcessor((el, ctx) => {
if (!this.data.allowMSSyntax) return;
Expand All @@ -365,48 +416,15 @@ export default class ObsidianAdmonition extends Plugin {
const firstLine = text[section.lineStart];
if (!/^> \[!.+\]/.test(firstLine)) return;

let [, type, title, col] = firstLine.match(MSDOCREGEX) ?? [];
const params = this.getMSParametersFromLine(firstLine);

if (
!type ||
(!this.admonitions[type] &&
!Object.keys(this.admonitions)
.map((k) => k.toLowerCase())
.includes(type.toLowerCase()))
)
return;
if (!(type in this.admonitions)) {
type = Object.keys(this.admonitions).find(
(k) => k.toLowerCase() == type.toLowerCase()
);
}
if (!type) return;
if (!params?.type) return;

let collapse;
switch (col) {
case "+": {
collapse = "open";
break;
}
case "-": {
collapse = "closed";
break;
}
case "x": {
break;
}
default: {
collapse = this.data.autoCollapse
? this.data.defaultCollapseType
: null;
}
}
const { type, title, collapse } = params;

const admonition = this.getAdmonitionElement(
type,
title ??
this.admonitions[type].title ??
`${type[0].toUpperCase()}${type.slice(1).toLowerCase()}`,
title,
this.admonitions[type].icon,
this.admonitions[type].color,
collapse
Expand Down Expand Up @@ -469,10 +487,7 @@ export default class ObsidianAdmonition extends Plugin {
toDOM(view: EditorView): HTMLElement {
const admonitionElement = self.getAdmonitionElement(
this.type,
this.title ??
self.admonitions[this.type].title ??
this.type[0].toUpperCase() +
this.type.slice(1).toLowerCase(),
this.title,
self.admonitions[this.type].icon,
self.admonitions[this.type].color,
this.collapse
Expand Down Expand Up @@ -626,32 +641,17 @@ export default class ObsidianAdmonition extends Plugin {
const line = split[0];
if (!/^> \[!.+\]/.test(line)) return;

const [, type, title, col] =
line.match(MSDOCREGEX) ?? [];

if (!type || !self.admonitions[type]) return;
let collapse;
switch (col) {
case "+": {
collapse = "open";
break;
}
case "-": {
collapse = "closed";
break;
}
case "x": {
break;
}
default: {
collapse = self.data.autoCollapse
? self.data.defaultCollapseType
: null;
}
}
const params =
self.getMSParametersFromLine(line);

if (!params?.type) return;

const { type, title, collapse } = params;

const end = split.findIndex(
(v) => !/^>/.test(v)
);

const content = split
.slice(1, end > -1 ? end : undefined)
.join("\n");
Expand Down

0 comments on commit ca3de23

Please sign in to comment.