From a355dd5b30439bd0454334b5b1f441a8156e0a40 Mon Sep 17 00:00:00 2001 From: akabeko Date: Thu, 29 Apr 2021 14:41:41 +0900 Subject: [PATCH] feat: Copy the section attributes from the heading, however the `id` will be moved --- src/plugins/section.ts | 62 ++++++++++++++++++++++++++---------------- tests/section.test.ts | 27 ++++++++++-------- 2 files changed, 54 insertions(+), 35 deletions(-) diff --git a/src/plugins/section.ts b/src/plugins/section.ts index 0bf9c26..b2eb739 100644 --- a/src/plugins/section.ts +++ b/src/plugins/section.ts @@ -13,13 +13,48 @@ import visit from 'unist-util-visit-parents'; /** Maximum depth of hierarchy to process headings. */ const MAX_HEADING_DEPTH = 6; +/** + * Check the heading properties to generate properties for the parent `
` and update the heading style. + * @param node Node of Markdown AST. + * @returns Properties. + */ +const checkProperties = (node: any, depth: number) => { + if (!node.data?.hProperties) { + return undefined; + } + + // Remove `id` attribute and copy otherwise for the parent `
` + const hProperties = { ...node.data.hProperties }; + if (node.data.hProperties.id) { + delete node.data.hProperties.id; + } + + // {hidden} specifier + if (Object.keys(hProperties).includes('hidden')) { + node.data.hProperties.style = 'display: none;'; + } + + // output section levels like Pandoc + if (Array.isArray(hProperties.class)) { + // Switch references as they do not affect the heading, + // and `remark-attr` may add classes, so make sure they come before them (always top) + const classes = [...hProperties.class]; + classes.unshift(`level${depth}`); + hProperties.class = classes; + } else { + hProperties.class = [`level${depth}`]; + } + + return hProperties; +}; + /** * Wrap the header in sections. * @param node Node of Markdown AST. * @param ancestors Parents. * @todo handle `@subtitle` properly. */ -function sectionize(node: any, ancestors: Parent[]) { +const sectionize = (node: any, ancestors: Parent[]) => { const start = node; const depth = start.depth; const parent = ancestors[ancestors.length - 1]; @@ -36,19 +71,7 @@ function sectionize(node: any, ancestors: Parent[]) { endIndex > 0 ? endIndex : undefined, ); - const type = 'section'; - - const hProperties = node.data?.hProperties; - if (hProperties) { - node.data.hProperties = {}; - - const props = Object.keys(hProperties); - - // {hidden} specifier - if (props.includes('hidden')) { - node.data.hProperties.style = 'display: none;'; - } - } + const hProperties = checkProperties(node, depth); const isDuplicated = parent.type === 'section'; if (isDuplicated) { @@ -61,14 +84,7 @@ function sectionize(node: any, ancestors: Parent[]) { return; } - // output section levels like Pandoc - if (Array.isArray(hProperties.class)) { - // `remark-attr` may add classes, so make sure they come before them (always top) - hProperties.class.unshift(`level${depth}`); - } else { - hProperties.class = [`level${depth}`]; - } - + const type = 'section'; const section = { type, data: { @@ -80,7 +96,7 @@ function sectionize(node: any, ancestors: Parent[]) { } as any; parent.children.splice(startIndex, section.children.length, section); -} +}; /** * Process Markdown AST. diff --git a/tests/section.test.ts b/tests/section.test.ts index fe9524f..f985e9e 100644 --- a/tests/section.test.ts +++ b/tests/section.test.ts @@ -10,11 +10,14 @@ it('plain section', () => { }); */ -it('

', () => { - const md = '# こんにちは {.test}'; - const received = stringify(md, { partial: true, disableFormatHtml: true }); - const expected = - '

こんにちは

'; +it('Leveling and copy attributes, however the `id` will be moved', () => { + const md = '# こんにちは {#id1 .class1 key1=value1}'; + const received = stringify(md, { partial: true }); + const expected = ` +
+

こんにちは

+
+`; expect(received).toBe(expected); }); @@ -67,17 +70,17 @@ it('

, ...

with attribute', () => { const received = stringify(md, { partial: true }); const expected = `
-

Heading 1

+

Heading 1

-

Heading 2

+

Heading 2

-

Heading 3

+

Heading 3

-

Heading 4

+

Heading 4

-
Heading 5
+
Heading 5
-
Heading 6
+
Heading 6
@@ -97,7 +100,7 @@ it('Complex structure', () => {

Heading 1

-

Heading 2

+

Heading 2