diff --git a/src/directives/admonitions.ts b/src/directives/admonitions.ts index 4511929..28d1752 100644 --- a/src/directives/admonitions.ts +++ b/src/directives/admonitions.ts @@ -27,11 +27,16 @@ class BaseAdmonition extends Directive { map: data.map, block: true }) - adToken.attrSet("class", "admonition") - if (this.title) + if (data.options.class?.length >= 1) { + // Custom class information must go first for styling + // For example, `class=tip, kind=seealso` should be styled as a `tip` + adToken.attrSet("class", data.options.class.join(" ")) + adToken.attrJoin("class", "admonition") + } else { + adToken.attrSet("class", "admonition") + } + if (this.title) { adToken.attrJoin("class", this.title.toLowerCase().replace(/ /g, "")) - if (data.options.class) { - adToken.attrJoin("class", data.options.class.join(" ")) } newTokens.push(adToken) diff --git a/src/directives/main.ts b/src/directives/main.ts index 848b85f..05bf374 100644 --- a/src/directives/main.ts +++ b/src/directives/main.ts @@ -182,11 +182,7 @@ export default function directiveToData( ;[body, options, bodyOffset] = parseDirectiveOptions(body, directive) } let args: string[] = [] - if ( - !directive.required_arguments && - !directive.optional_arguments && - !Object.keys(options).length - ) { + if (!directive.required_arguments && !directive.optional_arguments) { if (firstLine) { bodyOffset = 0 body = [firstLine].concat(body) diff --git a/tests/fixtures/directives.admonitions.md b/tests/fixtures/directives.admonitions.md new file mode 100644 index 0000000..cf77b69 --- /dev/null +++ b/tests/fixtures/directives.admonitions.md @@ -0,0 +1,216 @@ +Admonition: +. +```{admonition} This is a **title** +An example of an admonition with custom _title_. +``` +. + +. + +Note on split lines: +. +```{note} An example +of an admonition on two lines. +``` +. + +. + +[FIX] Note on a single line [See #154](https://github.com/executablebooks/MyST-Parser/issues/154) +. +```{danger} An example of an admonition on a single line. +``` +. + +. + +Admonition with overridding class name +. +```{admonition} This is a title +:class: tip +An example of a `tip` with a custom _title_. +``` +. + +. + +nested-admonition +. +````{note} This is a note +```{warning} This is a nested warning +``` +```` +. + +. + +`attention` admonition: +. +```{attention} +An example of a attention admonition. +``` +. + +. + +`caution` admonition: +. +```{caution} +An example of a caution admonition. +``` +. + +. + +`danger` admonition: +. +```{danger} +An example of a danger admonition. +``` +. + +. + +`error` admonition: +. +```{error} +An example of an error admonition. +``` +. + +. + +`hint` admonition: +. +```{hint} +An example of a hint admonition. +``` +. + +. + +`important` admonition: +. +```{important} +An example of an important admonition. +``` +. + +. + +`note` admonition: +. +```{note} +An example of a note admonition. +``` +. + +. + +`tip` admonition: +. +```{tip} +An example of a tip admonition. +``` +. + +. + +`warning` admonition: +. +```{warning} +An example of a warning admonition. +``` +. + +. + +`see also` admonition: +. +```{seealso} +See other things here! +``` +. + +. + + +`see also` admonition with class, bump title +. +```{seealso} Not a title +:class: tip +See other things here! +``` +. + +. + + +`see also` admonition with class, bump title new paragraph +. +```{seealso} Not a title +:class: tip + +See other things here! +``` +. + +. diff --git a/tests/fixtures/directives.md b/tests/fixtures/directives.md index ff4d31c..c90e350 100644 --- a/tests/fixtures/directives.md +++ b/tests/fixtures/directives.md @@ -10,47 +10,6 @@ content . -admonition -. -```{admonition} A **Title** -Some *content* -``` -. - -. - -admonition -. -```{seealso} -See other things here! -``` -. - -. - -nested-admonition -. -````{note} This is a note -```{warning} This is a nested warning -``` -```` -. - -. - image . ```{image} https://via.placeholder.com/150 diff --git a/tests/fixturesDirectives.spec.ts b/tests/fixturesDirectives.spec.ts index 7a7f319..c7015c9 100644 --- a/tests/fixturesDirectives.spec.ts +++ b/tests/fixturesDirectives.spec.ts @@ -7,7 +7,11 @@ describe("Parses directives", () => { readFixtures("directives").forEach(([name, text, expected]) => { const mdit = MarkdownIt().use(docutils_plugin) const rendered = mdit.render(text) - // console.log(rendered) + it(name, () => expect(rendered.trim()).toEqual((expected || "").trim())) + }) + readFixtures("directives.admonitions").forEach(([name, text, expected]) => { + const mdit = MarkdownIt().use(docutils_plugin) + const rendered = mdit.render(text) it(name, () => expect(rendered.trim()).toEqual((expected || "").trim())) }) }) @@ -20,7 +24,6 @@ describe("Parses math directives", () => { return `
\n${tokens[idx].content.trimRight()}\n
` } const rendered = mdit.render(text) - // console.log(rendered) it(name, () => expect(rendered.trim()).toEqual((expected || "").trim())) }) })