Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/tokenize/markWord.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import {flatMap} from 'lodash';
import {TokenizeEnhancer, TokenPath} from './interface';
import {leafOf, replace} from './utils';
import {leafOf, replace, isTextNode} from './utils';

function markInPaths(word: string, name: string, replacement: string) {
return (paths: TokenPath[]) => flatMap(
paths,
path => {
return (paths: TokenPath[]) =>
flatMap(paths, path => {
const leafNode = path.at(-1);

if (leafNode && !isTextNode(leafNode)) {
return [path];
}

const leaf = leafOf(path);

if (!leaf.value.includes(word)) {
Expand All @@ -29,7 +34,7 @@ function markInPaths(word: string, name: string, replacement: string) {
[]
);
}
);
);
}

export default function markWord(word: string, name: string, replacement = word): TokenizeEnhancer {
Expand Down
2 changes: 1 addition & 1 deletion src/tokenize/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function wrap(path: TokenPath, parent: ProcessingNode): TokenPath {
return [parent, ...clone(path)];
}

function isTextNode(node: ProcessingNode): node is TextNode {
export function isTextNode(node: ProcessingNode): node is TextNode {
return node.type === 'text';
}

Expand Down