Skip to content

Commit 99b3264

Browse files
authored
add index param to linkify callback (#68)
(for react key)
1 parent 2b50bf2 commit 99b3264

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/client/utils.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@
2020
export function linkify<D, T>(
2121
text: string,
2222
data: Record<string, D>,
23-
callback: (name: string, data: D) => T,
23+
callback: (name: string, data: D, index: number) => T,
2424
): Array<string | T> {
25-
return text.split(WORD_SEPARATORS).map(word => (data[word] == null ? word : callback(word, data[word])));
25+
return text
26+
.split(WORD_SEPARATORS)
27+
.map((word, idx) => (data[word] == null ? word : callback(word, data[word], idx)));
2628
}
29+
2730
const WORD_SEPARATORS = /([\[\]<>()| :.,]+)/g;
2831

29-
/** Slugify a string: "Really Cool Heading!" => "really-cool-heading-" */
32+
/**
33+
* Slugify a string: "Really Cool Heading!" => "really-cool-heading-"
34+
*/
3035
export function slugify(str: string) {
3136
return str.toLowerCase().replace(/[^\w.\/]/g, "-");
3237
}

0 commit comments

Comments
 (0)