Skip to content

Commit

Permalink
processAllNodes utility function simplifies many things
Browse files Browse the repository at this point in the history
  • Loading branch information
irskep committed Sep 9, 2024
1 parent 16d6cd0 commit 88f4ce9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 80 deletions.
6 changes: 5 additions & 1 deletion src/engine/djotFiltersPlus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,12 @@ const applyFilterToFragment = function (root: AstNode, filter: Filter): void {
}
};

function processAllNodes(doc: Doc, action: Action) {
applyFilter(doc, () => ({ "*": action }));
}

export type { Action, FilterPart, Filter, Transform };
export { applyFilter, applyFilterToFragment };
export { applyFilter, applyFilterToFragment, processAllNodes };

// Extra exports
export type { Walker };
Expand Down
50 changes: 24 additions & 26 deletions src/plugins/indextermsPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Block } from "@djot/djot";

import { DjockeyDoc, DjockeyPlugin } from "../types.js";
import { applyFilter } from "../engine/djotFiltersPlus.js";
import { applyFilter, processAllNodes } from "../engine/djotFiltersPlus.js";
import { getHasClass } from "../utils/djotUtils.js";
import { pushToList } from "../utils/collectionUtils.js";
import { LogCollector } from "../utils/logUtils.js";
Expand All @@ -23,32 +23,30 @@ export class IndextermsPlugin implements DjockeyPlugin {
const result: Record<string, { docRefPath: string; id: string }[]> = {};
for (const djotDoc of Object.values(doc.docs)) {
let nextID = 0;
applyFilter(djotDoc, () => ({
"*": (node) => {
if (!node.attributes) return;
const newNode = structuredClone(node);
if (!newNode.attributes) return;
let didFindIndexterm = false;
for (const k of Object.keys(node.attributes)) {
if (k.startsWith("indexterm")) {
const nodeID = newNode.attributes.id
? newNode.attributes.id
: `indexterm-${nextID++}`;
newNode.attributes.id = nodeID;
didFindIndexterm = true;
pushToList(result, node.attributes[k], {
docRefPath: doc.refPath,
id: nodeID,
});
}
}
if (didFindIndexterm) {
return newNode;
} else {
return;
processAllNodes(djotDoc, (node) => {
if (!node.attributes) return;
const newNode = structuredClone(node);
if (!newNode.attributes) return;
let didFindIndexterm = false;
for (const k of Object.keys(node.attributes)) {
if (k.startsWith("indexterm")) {
const nodeID = newNode.attributes.id
? newNode.attributes.id
: `indexterm-${nextID++}`;
newNode.attributes.id = nodeID;
didFindIndexterm = true;
pushToList(result, node.attributes[k], {
docRefPath: doc.refPath,
id: nodeID,
});
}
},
}));
}
if (didFindIndexterm) {
return newNode;
} else {
return;
}
});
}
// Reset this dict each time for idempotency in case we have multiple passes
this.indextermsByDoc[doc.refPath] = result;
Expand Down
60 changes: 28 additions & 32 deletions src/plugins/linkRewritingPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
DjockeyPlugin,
DjockeyRenderer,
} from "../types.js";
import { applyFilter } from "../engine/djotFiltersPlus.js";
import { applyFilter, processAllNodes } from "../engine/djotFiltersPlus.js";
import { pushToListIfNotPresent } from "../utils/collectionUtils.js";
import { LogCollector } from "../utils/logUtils.js";
import { fsjoin, CANONICAL_SEPARATOR, refsplit } from "../utils/pathUtils.js";
Expand Down Expand Up @@ -63,14 +63,12 @@ export class LinkRewritingPlugin implements DjockeyPlugin {
registerLinkTarget(new MappableLinkTarget(doc, null));

for (const djotDoc of Object.values(doc.docs)) {
applyFilter(djotDoc, () => ({
"*": (node) => {
const attrs = { ...node.autoAttributes, ...node.attributes };
if (!attrs.id) return;

registerLinkTarget(new MappableLinkTarget(doc, attrs.id));
},
}));
processAllNodes(djotDoc, (node) => {
const attrs = { ...node.autoAttributes, ...node.attributes };
if (!attrs.id) return;

registerLinkTarget(new MappableLinkTarget(doc, attrs.id));
});
}
}

Expand All @@ -82,31 +80,29 @@ export class LinkRewritingPlugin implements DjockeyPlugin {
}) {
const { doc, renderer, config, logCollector } = args;
for (const djotDoc of Object.values(doc.docs)) {
applyFilter(djotDoc, () => ({
"*": (node) => {
if (!node.destination) return;
processAllNodes(djotDoc, (node) => {
if (!node.destination) return;

const defaultLabel = this._defaultLinkLabels[node.destination];

const newDestination = this.transformNodeDestination({
sourcePath: doc.refPath,
inputRoot: config.input_dir,
unresolvedNodeDestination: node.destination,
renderArgs: {
config: this.config,
renderer,
sourcePath: doc.refPath,
logCollector,
},
});

const defaultLabel = this._defaultLinkLabels[node.destination];
const children = [...(node.children ?? [])];
if (!children.length && defaultLabel)
children.push({ tag: "str", text: defaultLabel });

const newDestination = this.transformNodeDestination({
sourcePath: doc.refPath,
inputRoot: config.input_dir,
unresolvedNodeDestination: node.destination,
renderArgs: {
config: this.config,
renderer,
sourcePath: doc.refPath,
logCollector,
},
});

const children = [...(node.children ?? [])];
if (!children.length && defaultLabel)
children.push({ tag: "str", text: defaultLabel });

return { ...node, destination: newDestination, children };
},
}));
return { ...node, destination: newDestination, children };
});
}
}

Expand Down
36 changes: 15 additions & 21 deletions src/utils/djotUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
HasText,
isBlock,
} from "@djot/djot";
import { applyFilter } from "../engine/djotFiltersPlus.js";
import { applyFilter, processAllNodes } from "../engine/djotFiltersPlus.js";

export function getHasClass(node: HasAttributes, cls: string): boolean {
if (!node.attributes || !node.attributes["class"]) return false;
Expand Down Expand Up @@ -47,27 +47,23 @@ export function makeStubDjotDoc(children: Block[]): Doc {

export function djotASTToText(children: Block[]) {
const result = new Array<string>();
applyFilter(makeStubDjotDoc(children), () => ({
"*": (node: HasText) => {
if (!node.text) return;
result.push(node.text);
},
}));
processAllNodes(makeStubDjotDoc(children), (node) => {
if (!node.text) return;
result.push(node.text);
});
return result.join("");
}

export function djotASTToTextWithLineBreaks(children: Block[]) {
const result = new Array<string>();

applyFilter(makeStubDjotDoc(children), () => ({
"*": (node: AstNode) => {
const text = (node as HasText).text;
result.push(text ?? "");
if (isBlock(node)) {
result.push("\n\n");
}
},
}));
processAllNodes(makeStubDjotDoc(children), (node) => {
const text = (node as HasText).text;
result.push(text ?? "");
if (isBlock(node)) {
result.push("\n\n");
}
});
return result.join("").replace(/\n\n+/g, "\n\n");
}

Expand All @@ -77,11 +73,9 @@ export function djotASTContainsNode(
): boolean {
let result = false;

applyFilter(doc, () => ({
"*": (node: AstNode) => {
result = result || predicate(node);
},
}));
processAllNodes(doc, (node) => {
result = result || predicate(node);
});

return result;
}

0 comments on commit 88f4ce9

Please sign in to comment.