Skip to content

Commit

Permalink
fix: the role is always one
Browse files Browse the repository at this point in the history
  • Loading branch information
akabekobeko committed Oct 15, 2023
1 parent 7e5e925 commit 66d0f15
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"release:pre": "release-it --preRelease --npm.tag=latest",
"test": "jest",
"test:watch": "jest --runInBand --watch",
"test:debug": "jest tests/xxx.test.ts --silent=false --verbose false"
"test:debug": "jest tests/wai-aria-role.test.ts --silent=false --verbose false"
},
"dependencies": {
"debug": "^4.3.1",
Expand Down
13 changes: 8 additions & 5 deletions src/plugins/wai-aria-role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ const visitor: Visitor<Element> = (node) => {
return;
}

// Skip if the role already exists.
if (node.properties.role) {
return;
}

// Set the first target class found to role.
for (const className of node.properties.className) {
if (typeof className === 'string' && targetClassNames.includes(className)) {
if (Array.isArray(node.properties.role)) {
node.properties.role.push(`doc-${className}`);
} else {
node.properties.role = [`doc-${className}`];
}
node.properties.role = `doc-${className}`;
break;
}
}
};
Expand Down
11 changes: 11 additions & 0 deletions tests/wai-aria-role.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@ it('doc-chapter', () => {
const expected = `<section class="level1" aria-labelledby="example"><h1 class="example" id="example" role="doc-example">Example</h1></section>`;
expect(received).toBe(expected);
});

it('If the role already exists, skip adding it.', () => {
const md = `# Appendix {.appendix}
<h2 class="example" role="doc-appendix">Example 1</h2>
<h2 class="example">Example 2</h2>
`;
const received = stringify(md, options);
const expected = `<section class="level1" aria-labelledby="appendix"><h1 class="appendix" id="appendix" role="doc-appendix">Appendix</h1><h2 class="example" role="doc-appendix">Example 1</h2>
<h2 class="example" role="doc-example">Example 2</h2></section>`;
expect(received).toBe(expected);
});

0 comments on commit 66d0f15

Please sign in to comment.