Skip to content

Commit 76e62f7

Browse files
authored
fix(visual): truncate headings, escape HTML in orama (#460)
1 parent 3fd0072 commit 76e62f7

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

src/generators/orama-db/index.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ export default {
7777

7878
return {
7979
title: hierarchicalTitle,
80-
description: paragraph ? transformNodeToString(paragraph) : undefined,
80+
description: paragraph
81+
? transformNodeToString(paragraph, true)
82+
: undefined,
8183
path: `${entry.api}.html#${entry.slug}`,
8284
};
8385
})

src/generators/web/ui/index.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ main {
3434
align-items: center;
3535
gap: 8px;
3636

37+
> h1,
38+
> h2,
39+
> h3,
40+
> h4,
41+
> h5,
42+
> h6 {
43+
flex: 1;
44+
text-overflow: ellipsis;
45+
overflow: hidden;
46+
white-space: nowrap;
47+
}
48+
3749
.change-history {
3850
margin-left: auto;
3951
}

src/utils/unist.mjs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,37 @@
22

33
import { pointEnd, pointStart } from 'unist-util-position';
44

5+
/**
6+
* Escapes HTML entities ("<" and ">") in a string
7+
* @param {string} string The string
8+
*/
9+
const escapeHTMLEntities = string =>
10+
string.replace(/</g, '&lt;').replace(/>/g, '&gt;');
11+
512
/**
613
* Extracts text content from a node recursively
714
*
815
* @param {import('unist').Node} node The Node to be transformed into a string
16+
* @param {boolean} [escape] Escape HTML entities ("<", ">")?
917
* @returns {string} The transformed Node as a string
1018
*/
11-
export const transformNodeToString = node => {
19+
export const transformNodeToString = (node, escape) => {
1220
switch (node.type) {
1321
case 'inlineCode':
14-
return `\`${node.value}\``;
22+
return `\`${escape ? escapeHTMLEntities(node.value) : node.value}\``;
1523
case 'strong':
16-
return `**${transformNodesToString(node.children)}**`;
24+
return `**${transformNodesToString(node.children, escape)}**`;
1725
case 'emphasis':
18-
return `_${transformNodesToString(node.children)}_`;
26+
return `_${transformNodesToString(node.children, escape)}_`;
1927
default: {
2028
if (node.children) {
21-
return transformNodesToString(node.children);
29+
return transformNodesToString(node.children, escape);
2230
}
2331

32+
const string = node.value?.replace(/\n/g, ' ') || '';
33+
2434
// Replace line breaks (\n) with spaces to keep text in a single line
25-
return node.value?.replace(/\n/g, ' ') || '';
35+
return escape ? escapeHTMLEntities(string) : string;
2636
}
2737
}
2838
};
@@ -32,10 +42,11 @@ export const transformNodeToString = node => {
3242
* and transfor them back to what their source would look like
3343
*
3444
* @param {Array<import('unist').Parent & import('unist').Literal>} nodes Nodes to parsed and joined
45+
* @param {boolean} [escape] Escape HTML entities ("<", ">")?
3546
* @returns {string} The parsed and joined nodes as a string
3647
*/
37-
export const transformNodesToString = nodes => {
38-
const mappedChildren = nodes.map(node => transformNodeToString(node));
48+
export const transformNodesToString = (nodes, escape) => {
49+
const mappedChildren = nodes.map(node => transformNodeToString(node, escape));
3950

4051
return mappedChildren.join('');
4152
};

0 commit comments

Comments
 (0)