From 63959daf83358ca13c8ad864885b6e6f5326802a Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 30 May 2023 10:59:40 -0400 Subject: [PATCH 1/2] Fix lint error (#37338) --- tests/browser/browser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/browser/browser.js b/tests/browser/browser.js index 1c9006fff2..af7b9aa279 100644 --- a/tests/browser/browser.js +++ b/tests/browser/browser.js @@ -2,7 +2,7 @@ import { jest } from '@jest/globals' jest.useFakeTimers({ legacyFakeTimers: true }) -/* global page, browser */ +/* global page */ describe('homepage', () => { jest.setTimeout(60 * 1000) From 76ddf3a70c02f30adab31f08aca90959644fcad6 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 30 May 2023 11:06:08 -0400 Subject: [PATCH 2/2] Refactor Thead Th scope Markdown plugin (#37299) --- lib/render-content/create-processor.js | 26 ++------------ .../plugins/rewrite-thead-th-scope.js | 35 +++++++++++++++++++ package-lock.json | 2 +- package.json | 2 +- 4 files changed, 39 insertions(+), 26 deletions(-) create mode 100644 lib/render-content/plugins/rewrite-thead-th-scope.js diff --git a/lib/render-content/create-processor.js b/lib/render-content/create-processor.js index 6535b994ac..c89eb3395b 100644 --- a/lib/render-content/create-processor.js +++ b/lib/render-content/create-processor.js @@ -22,6 +22,7 @@ import rewriteAssetImgTags from './plugins/rewrite-asset-img-tags.js' import useEnglishHeadings from './plugins/use-english-headings.js' import wrapInElement from './plugins/wrap-in-element.js' import headingLinks from './plugins/heading-links.js' +import rewriteTheadThScope from './plugins/rewrite-thead-th-scope.js' // plugins aren't designed to be used more than once, // this workaround lets us do that @@ -29,28 +30,6 @@ import headingLinks from './plugins/heading-links.js' const wrapperForImages = () => wrapInElement({ selector: 'ol > li img', wrapper: 'span.procedural-image-wrapper' }) -const wrapperForTheadThScope = () => - wrapInElement({ - selector: 'thead th', - wrapper: 'th', - // This moves what it finds in the selector inside the wrapper. - rewrite: true, - wrapperAdditionalAttributes: { - scope: 'col', - }, - }) - -const wrapperForTbodyThScope = () => - wrapInElement({ - selector: 'thead th', - wrapper: 'th', - // This moves what it finds in the selector inside the wrapper. - rewrite: true, - wrapperAdditionalAttributes: { - scope: 'col', - }, - }) - const wrapperForRowheadersTbody = () => wrapInElement({ selector: '.rowheaders tbody tr td:first-child', @@ -78,8 +57,7 @@ export default function createProcessor(context) { }) .use(raw) .use(wrapperForImages) - .use(wrapperForTheadThScope) - .use(wrapperForTbodyThScope) + .use(rewriteTheadThScope) .use(wrapperForRowheadersTbody) .use(rewriteImgSources) .use(rewriteAssetImgTags) diff --git a/lib/render-content/plugins/rewrite-thead-th-scope.js b/lib/render-content/plugins/rewrite-thead-th-scope.js new file mode 100644 index 0000000000..e5cd5c4d81 --- /dev/null +++ b/lib/render-content/plugins/rewrite-thead-th-scope.js @@ -0,0 +1,35 @@ +import { visitParents } from 'unist-util-visit-parents' + +/** + * Where it can mutate the AST to swap from: + * + * + * + * ... + * ... + * + * to: + * + * + * ... + * ... + * + * */ + +function matcher(node) { + return node.type === 'element' && node.tagName === 'th' && !('scope' in node.properties) +} + +function visitor(node, ancestors) { + const parent = ancestors.at(-1) + if (parent && parent.tagName === 'tr') { + const grandParent = ancestors.at(-2) + if (grandParent && grandParent.tagName === 'thead') { + node.properties.scope = 'col' + } + } +} + +export default function rewriteTheadThScope() { + return (tree) => visitParents(tree, matcher, visitor) +} diff --git a/package-lock.json b/package-lock.json index 66186de29a..d7ba0132f1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -161,7 +161,7 @@ "start-server-and-test": "^2.0.0", "typescript": "^5.0.2", "unist-util-remove": "^3.1.1", - "unist-util-visit-parents": "^5.1.3", + "unist-util-visit-parents": "5.1.3", "website-scraper": "^5.3.1" }, "engines": { diff --git a/package.json b/package.json index 0973e5a456..3df1110866 100644 --- a/package.json +++ b/package.json @@ -163,7 +163,7 @@ "start-server-and-test": "^2.0.0", "typescript": "^5.0.2", "unist-util-remove": "^3.1.1", - "unist-util-visit-parents": "^5.1.3", + "unist-util-visit-parents": "5.1.3", "website-scraper": "^5.3.1" }, "engines": {