-
-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: disable attributes copy to parent #157
Changes from all commits
f7d4cd7
7ef0d5a
d0e0820
e9dcc38
f87c840
e4ec2df
6396aa3
fe85733
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,46 +14,26 @@ import visit from 'unist-util-visit-parents'; | |
const MAX_HEADING_DEPTH = 6; | ||
|
||
/** | ||
* Check the heading properties to generate properties for the parent `<section>` and update the heading style. | ||
* @param node Node of Markdown AST. | ||
* Create the attribute properties of a section. | ||
* @param depth - Depth of heading elements that are sections. | ||
* @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 `<section>` | ||
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.hidden = 'hidden'; | ||
} | ||
const createProperties = (depth: number, node: any): KeyValue => { | ||
const properties: KeyValue = { | ||
class: [`level${depth}`], | ||
}; | ||
|
||
// 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}`]; | ||
if (node?.data?.hProperties?.id) { | ||
properties['aria-labelledby'] = node?.data.hProperties.id; | ||
} | ||
|
||
return hProperties; | ||
return properties; | ||
}; | ||
|
||
/** | ||
* Wrap the header in sections. | ||
* - Do not sectionize if parent is `blockquote`. | ||
* - The attributes of the heading are basically copied to the section. | ||
* - The `id` attribute is moved to the section. | ||
* - The `hidden` attribute is not copied and only the heading applies. | ||
* - Set the `levelN` class in the section to match the heading depth. | ||
* @param node Node of Markdown AST. | ||
* @param ancestors Parents. | ||
|
@@ -80,7 +60,12 @@ const sectionize = (node: any, ancestors: Parent[]) => { | |
endIndex > 0 ? endIndex : undefined, | ||
); | ||
|
||
const hProperties = checkProperties(node, depth); | ||
const hProperties = createProperties(depth, node); | ||
|
||
// {hidden} specifier | ||
if (Object.keys(node.data.hProperties).includes('hidden')) { | ||
node.data.hProperties.hidden = 'hidden'; | ||
} | ||
Comment on lines
+65
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
const isDuplicated = parent.type === 'section'; | ||
if (isDuplicated) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,28 @@ | ||
import { stripIndent } from 'common-tags'; | ||
import { buildProcessorTestingCode } from './utils'; | ||
import { stringify } from '../src/index'; | ||
|
||
it( | ||
'Heading with attributes', | ||
buildProcessorTestingCode( | ||
`# Heading {#foo}`, | ||
stripIndent` | ||
root[1] | ||
└─0 heading[1] | ||
│ depth: 1 | ||
│ data: {"hProperties":{"id":"foo"}} | ||
└─0 text "Heading" | ||
`, | ||
`<section id="foo" class="level1"><h1>Heading</h1></section>`, | ||
), | ||
); | ||
it('Heading with attributes', () => { | ||
const received = stringify('# Heading {#foo}', { | ||
partial: true, | ||
disableFormatHtml: true, | ||
}); | ||
const expected = `<section class="level1" aria-labelledby="foo"><h1 id="foo">Heading</h1></section>`; | ||
expect(received).toBe(expected); | ||
}); | ||
|
||
it( | ||
'Heading with attributes, specification by line break', | ||
buildProcessorTestingCode( | ||
`# Heading\n{#foo}`, | ||
stripIndent` | ||
root[1] | ||
└─0 heading[1] | ||
│ depth: 1 | ||
│ data: {"hProperties":{"id":"foo"}} | ||
└─0 text "Heading" | ||
`, | ||
`<section id="foo" class="level1"><h1>Heading</h1></section>`, | ||
), | ||
); | ||
it('Heading with attributes, specification by line break', () => { | ||
const received = stringify('# Heading\n{#foo}', { | ||
partial: true, | ||
disableFormatHtml: true, | ||
}); | ||
const expected = `<section class="level1" aria-labelledby="foo"><h1 id="foo">Heading</h1></section>`; | ||
expect(received).toBe(expected); | ||
}); | ||
|
||
it( | ||
'Heading with attributes and inline elements, specification by line break', | ||
buildProcessorTestingCode( | ||
`# Heading *test*\n{#foo}`, | ||
stripIndent` | ||
root[1] | ||
└─0 heading[2] | ||
│ depth: 1 | ||
│ data: {"hProperties":{"id":"foo"}} | ||
├─0 text "Heading " | ||
└─1 emphasis[1] | ||
└─0 text "test" | ||
`, | ||
`<section id="foo" class="level1"><h1>Heading <em>test</em></h1></section>`, | ||
), | ||
); | ||
|
||
// `remark-attr` needs to be fixed | ||
// https://github.com/arobase-che/remark-attr/issues/24 | ||
/* | ||
it( | ||
'Heading with attributes and inline elements', | ||
buildProcessorTestingCode( | ||
`# Heading *test* {#foo}`, | ||
stripIndent` | ||
root[1] | ||
└─0 heading[2] | ||
│ depth: 1 | ||
│ data: {"hProperties":{"id":"foo"}} | ||
├─0 text "Heading " | ||
└─1 emphasis[1] | ||
└─0 text "test" | ||
`, | ||
`<section id="foo"><h1>Heading <em>test</em></h1></section>`, | ||
), | ||
); | ||
*/ | ||
it('Heading with attributes and inline elements, specification by line break', () => { | ||
const received = stringify('# Heading *test*\n{#foo}', { | ||
partial: true, | ||
disableFormatHtml: true, | ||
}); | ||
const expected = `<section class="level1" aria-labelledby="foo"><h1 id="foo">Heading <em>test</em></h1></section>`; | ||
expect(received).toBe(expected); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 仕様変更によるテスト修正です。 あわせて utils の利用を廃止しました。utils は整形された MDAST と HTML の両方を判定するのですが以下の理由により利用をやめてゆきます。
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
属性コピーを無効化したので関連する記述を削除しました。