Skip to content

Commit c97700d

Browse files
committed
Use hast-util-find-parent instead of internal selectParent
The first of perhaps many util functions and plugins to be spun out into their own modules
1 parent 7dbdc6e commit c97700d

File tree

5 files changed

+15
-27
lines changed

5 files changed

+15
-27
lines changed

scripts/doc-processor/package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/doc-processor/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"author": "",
1212
"license": "ISC",
1313
"dependencies": {
14+
"hast-util-find-parent": "^1.0.2",
1415
"hast-util-select": "^6.0.2",
1516
"hastscript": "^9.0.0",
1617
"rehype-format": "^5.0.0",

scripts/doc-processor/src/unified-plugins/imageTitlesToCaptions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {select, selectAll} from 'hast-util-select';
22
import {h} from 'hastscript';
3-
import {selectParent} from './shared.js';
3+
import {findParent} from 'hast-util-find-parent';
44

55
/**
66
* A plugin that takes image titles and turns them into figcaptions.
@@ -32,7 +32,7 @@ export default function imageTitlesToCaptions() {
3232
h('figcaption', title)
3333
]);
3434

35-
const pictureParentEl = selectParent(pictureEl, tree);
35+
const pictureParentEl = findParent(pictureEl, tree);
3636

3737
// Replace the picture element in the tree with the new figure element.
3838
pictureParentEl.children.splice(

scripts/doc-processor/src/unified-plugins/optimizeImages.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import fs from 'node:fs/promises';
66
import {h} from 'hastscript';
77
import sharp from 'sharp';
88
import {selectAll} from 'hast-util-select';
9-
import {selectParent} from './shared.js';
9+
import {findParent} from 'hast-util-find-parent';
1010
import {toHashedFilename} from '../util.js';
1111

1212
/**
@@ -98,7 +98,7 @@ export default function optimizeImages({
9898
return async tree => {
9999
for (const imgEl of selectAll('main img', tree)) {
100100
const pathToImage = toDiskPath(imgEl.properties.src);
101-
const parentEl = selectParent(imgEl, tree);
101+
const parentEl = findParent(imgEl, tree);
102102

103103
// read in the original file and compute its hash.
104104
const hash = createHash('sha256');

scripts/doc-processor/src/unified-plugins/shared.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,6 @@ export function applyDateToTime(timeEl, date) {
1717
});
1818
}
1919

20-
/**
21-
* Find the `Element` that has the given `element` in the given `tree`.
22-
* @param {import('hast').Element} element
23-
* @param {import('hast').Element} tree
24-
* @returns {import('hast').Element | null} The parent of `element`.
25-
*/
26-
export function selectParent(element, tree) {
27-
if (!tree || !Array.isArray(tree.children)) {
28-
return null;
29-
}
30-
if (tree.children.includes(element)) {
31-
return tree;
32-
}
33-
for (const childTree of tree.children) {
34-
const parent = selectParent(element, childTree);
35-
36-
if (parent !== null) {
37-
return parent;
38-
}
39-
}
40-
return null;
41-
}
42-
4320
/**
4421
*
4522
* @param {import('hast').Element} elementToRemove

0 commit comments

Comments
 (0)