asma-core-helpers 0.22.0 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Rich-text values (TipTap output) are stored as HTML strings. Consumers that are not rich-text
3
+ * interpreters — plain inputs, titles, document merge fields — need to tell markup from ordinary
4
+ * text and render it readably. These helpers are the single implementation of both.
5
+ *
6
+ * No DOM: this runs in the browser apps and in the Bun backend that generates documents.
7
+ */
8
+ /**
9
+ * Whether a value should be treated as an HTML fragment.
10
+ *
11
+ * Conservative by design — a false positive corrupts a legitimate plain value, while a false
12
+ * negative only leaves today's behaviour unchanged.
13
+ */
14
+ export declare function isHtmlFragment(value: string | null | undefined): boolean;
15
+ /**
16
+ * Renders an HTML fragment as readable plain text, preserving structure: blocks and `<br>` become
17
+ * newlines and list items gain a leading dash.
18
+ *
19
+ * Values that are not markup are returned **byte-identical** — callers may pass anything.
20
+ */
21
+ export declare function htmlToPlainText(value: string | null | undefined): string;
22
+ //# sourceMappingURL=htmlText.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"htmlText.d.ts","sourceRoot":"","sources":["../../src/helpers/htmlText.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAqEH;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAIxE;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,CAexE"}
@@ -0,0 +1,101 @@
1
+ /**
2
+ * Rich-text values (TipTap output) are stored as HTML strings. Consumers that are not rich-text
3
+ * interpreters — plain inputs, titles, document merge fields — need to tell markup from ordinary
4
+ * text and render it readably. These helpers are the single implementation of both.
5
+ *
6
+ * No DOM: this runs in the browser apps and in the Bun backend that generates documents.
7
+ */
8
+ /** The subset TipTap's StarterKit emits, plus the containers it wraps them in. */
9
+ const KNOWN_TAGS = [
10
+ 'p',
11
+ 'br',
12
+ 'div',
13
+ 'span',
14
+ 'b',
15
+ 'strong',
16
+ 'i',
17
+ 'em',
18
+ 'u',
19
+ 's',
20
+ 'strike',
21
+ 'del',
22
+ 'ul',
23
+ 'ol',
24
+ 'li',
25
+ 'h1',
26
+ 'h2',
27
+ 'h3',
28
+ 'h4',
29
+ 'h5',
30
+ 'h6',
31
+ 'blockquote',
32
+ 'pre',
33
+ 'code',
34
+ 'a',
35
+ // Not emitted by StarterKit, but pasted content and legacy values carry tables; without them
36
+ // a table would fail the sniff and reach the user as visible markup.
37
+ 'table',
38
+ 'thead',
39
+ 'tbody',
40
+ 'tr',
41
+ 'td',
42
+ 'th',
43
+ ];
44
+ /**
45
+ * Matches an opening/closing/self-closing tag from KNOWN_TAGS only.
46
+ *
47
+ * Deliberately narrow: legacy values carry RTF-conversion artifacts (`<å711,0,0>`) and document
48
+ * merge placeholders (`<Pasientnavn>`) that are bracket-shaped but must never be treated as markup.
49
+ */
50
+ const KNOWN_TAG_PATTERN = new RegExp(String.raw `<\s*/?\s*(?:${KNOWN_TAGS.join('|')})(?:\s[^<>]*)?/?\s*>`, 'i');
51
+ /** Any tag-shaped run, used for stripping once a value is known to be markup. */
52
+ const ANY_TAG_PATTERN = /<[^<>]*>/g;
53
+ const BLOCK_BOUNDARY_PATTERN = /<\s*\/?\s*(?:p|div|h[1-6]|blockquote|pre|ul|ol|tr)(?:\s[^<>]*)?\/?\s*>/gi;
54
+ const LINE_BREAK_PATTERN = /<\s*br(?:\s[^<>]*)?\/?\s*>/gi;
55
+ const LIST_ITEM_PATTERN = /<\s*li(?:\s[^<>]*)?\s*>/gi;
56
+ /** Cells are separated rather than newline-split, so a row stays one line. */
57
+ const TABLE_CELL_PATTERN = /<\s*\/\s*(?:td|th)\s*>(?=\s*<\s*(?:td|th)[\s>])/gi;
58
+ const ENTITIES = [
59
+ [/&nbsp;/gi, ' '],
60
+ [/&lt;/gi, '<'],
61
+ [/&gt;/gi, '>'],
62
+ [/&quot;/gi, '"'],
63
+ [/&#0*39;|&apos;/gi, "'"],
64
+ // Ampersand last, so `&amp;lt;` decodes to `&lt;` rather than to `<`.
65
+ [/&amp;/gi, '&'],
66
+ ];
67
+ const decodeEntities = (value) => ENTITIES.reduce((acc, [pattern, replacement]) => acc.replace(pattern, replacement), value);
68
+ /**
69
+ * Whether a value should be treated as an HTML fragment.
70
+ *
71
+ * Conservative by design — a false positive corrupts a legitimate plain value, while a false
72
+ * negative only leaves today's behaviour unchanged.
73
+ */
74
+ export function isHtmlFragment(value) {
75
+ if (typeof value !== 'string' || value.length === 0)
76
+ return false;
77
+ return KNOWN_TAG_PATTERN.test(value);
78
+ }
79
+ /**
80
+ * Renders an HTML fragment as readable plain text, preserving structure: blocks and `<br>` become
81
+ * newlines and list items gain a leading dash.
82
+ *
83
+ * Values that are not markup are returned **byte-identical** — callers may pass anything.
84
+ */
85
+ export function htmlToPlainText(value) {
86
+ if (typeof value !== 'string' || value.length === 0)
87
+ return '';
88
+ if (!isHtmlFragment(value))
89
+ return value;
90
+ const withStructure = value
91
+ .replace(LINE_BREAK_PATTERN, '\n')
92
+ .replace(LIST_ITEM_PATTERN, '\n- ')
93
+ .replace(TABLE_CELL_PATTERN, '\t')
94
+ .replace(BLOCK_BOUNDARY_PATTERN, '\n');
95
+ return decodeEntities(withStructure.replace(ANY_TAG_PATTERN, ''))
96
+ .split('\n')
97
+ .map((line) => line.trim())
98
+ .filter((line) => line.length > 0 && line !== '-')
99
+ .join('\n');
100
+ }
101
+ //# sourceMappingURL=htmlText.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"htmlText.js","sourceRoot":"","sources":["../../src/helpers/htmlText.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,kFAAkF;AAClF,MAAM,UAAU,GAAG;IACf,GAAG;IACH,IAAI;IACJ,KAAK;IACL,MAAM;IACN,GAAG;IACH,QAAQ;IACR,GAAG;IAC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cAAc,CAAC,KAAgC;IAC3D,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,KAAK,CAAA;IAEjE,OAAO,iBAAiB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;AACxC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,KAAgC;IAC5D,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAA;IAC9D,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAA;IAExC,MAAM,aAAa,GAAG,KAAK;SACtB,OAAO,CAAC,kBAAkB,EAAE,IAAI,CAAC;SACjC,OAAO,CAAC,iBAAiB,EAAE,MAAM,CAAC;SAClC,OAAO,CAAC,kBAAkB,EAAE,IAAI,CAAC;SACjC,OAAO,CAAC,sBAAsB,EAAE,IAAI,CAAC,CAAA;IAE1C,OAAO,cAAc,CAAC,aAAa,CAAC,OAAO,CAAC,eAAe,EAAE,EAAE,CAAC,CAAC;SAC5D,KAAK,CAAC,IAAI,CAAC;SACX,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;SAC1B,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,IAAI,KAAK,GAAG,CAAC;SACjD,IAAI,CAAC,IAAI,CAAC,CAAA;AACnB,CAAC"}
package/lib/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export * from './helpers/CapitalizeFistLetter.js';
5
5
  export * from './helpers/IsAdcuris.js';
6
6
  export * from './helpers/IsAdopus.js';
7
7
  export * from './helpers/IsNotEmpty.js';
8
+ export * from './helpers/htmlText.js';
8
9
  export * from './helpers/Config.js';
9
10
  export * from './helpers/InitializeIDBListenersOnMstSnapshots.js';
10
11
  export * from './helpers/initEnv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AA;AAC5D,cAAc,+CAA+C,CAAA;AAC7D,cAAc,mCAAmC,CAAA;AACjD,cAAc,qBAAqB,CAAA;AACnC,cAAc,uCAAuC,CAAA;AACrD,cAAc,gCAAgC,CAAA;AAC9C,cAAc,kCAAkC,CAAA;AAEhD,OAAO,EAAE,uBAAuB,EAAE,MAAM,sCAAsC,CAAA;AAC9E,YAAY,EAAE,8BAA8B,EAAE,gBAAgB,EAAE,MAAM,sCAAsC,CAAA;AAC5G,OAAO,EAAE,qBAAqB,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAA;AACzF,YAAY,EAAE,4BAA4B,EAAE,MAAM,oCAAoC,CAAA"}
package/lib/index.js CHANGED
@@ -5,6 +5,7 @@ export * from './helpers/CapitalizeFistLetter.js';
5
5
  export * from './helpers/IsAdcuris.js';
6
6
  export * from './helpers/IsAdopus.js';
7
7
  export * from './helpers/IsNotEmpty.js';
8
+ export * from './helpers/htmlText.js';
8
9
  export * from './helpers/Config.js';
9
10
  export * from './helpers/InitializeIDBListenersOnMstSnapshots.js';
10
11
  export * from './helpers/initEnvConfigsVars.js';
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kCAAkC,CAAA;AAChD,cAAc,oCAAoC,CAAA;AAClD,cAAc,mCAAmC,CAAA;AACjD,cAAc,mCAAmC,CAAA;AACjD,cAAc,wBAAwB,CAAA;AACtC,cAAc,uBAAuB,CAAA;AACrC,cAAc,yBAAyB,CAAA;AACvC,cAAc,qBAAqB,CAAA;AACnC,cAAc,mDAAmD,CAAA;AACjE,cAAc,iCAAiC,CAAA;AAE/C,cAAc,kCAAkC,CAAA;AAChD,cAAc,uBAAuB,CAAA;AACrC,cAAc,+BAA+B,CAAA;AAC7C,cAAc,wCAAwC,CAAA;AACtD,cAAc,sCAAsC,CAAA;AACpD,cAAc,4CAA4C,CAAA;AAC1D,cAAc,2BAA2B,CAAA;A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CAAwC,CAAA;AACtD,cAAc,sCAAsC,CAAA;AACpD,cAAc,4CAA4C,CAAA;AAC1D,cAAc,2BAA2B,CAAA;AACzC,cAAc,2CAA2C,CAAA;AACzD,cAAc,6BAA6B,CAAA;AAC3C,cAAc,yCAAyC,CAAA;AACvD,cAAc,+BAA+B,CAAA;AAC7C,cAAc,2BAA2B,CAAA;AACzC,cAAc,yBAAyB,CAAA;AACvC,cAAc,2CAA2C,CAAA;AACzD,cAAc,gCAAgC,CAAA;AAC9C,cAAc,iCAAiC,CAAA;AAC/C,cAAc,gCAAgC,CAAA;AAC9C,cAAc,+BAA+B,CAAA;AAC7C,gDAAgD;AAChD,cAAc,qBAAqB,CAAA;AACnC,cAAc,kCAAkC,CAAA;AAEhD,cAAc,oBAAoB,CAAA;AAElC,cAAc,sBAAsB,CAAA;AAEpC,cAAc,uBAAuB,CAAA;AAErC,cAAc,oBAAoB,CAAA;AAElC,cAAc,8CAA8C,CAAA;AAC5D,cAAc,+CAA+C,CAAA;AAC7D,cAAc,mCAAmC,CAAA;AACjD,cAAc,qBAAqB,CAAA;AACnC,cAAc,uCAAuC,CAAA;AACrD,cAAc,gCAAgC,CAAA;AAC9C,cAAc,kCAAkC,CAAA;AAEhD,OAAO,EAAE,uBAAuB,EAAE,MAAM,sCAAsC,CAAA;AAE9E,OAAO,EAAE,qBAAqB,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "asma-core-helpers",
3
- "version": "0.22.0",
3
+ "version": "0.23.0",
4
4
  "description": "Core helper utilities for ASMA applications",
5
5
  "files": [
6
6
  "lib/**/*",
@@ -0,0 +1,116 @@
1
+ import assert from 'node:assert/strict'
2
+ import { describe, it } from 'node:test'
3
+
4
+ import { htmlToPlainText, isHtmlFragment } from './htmlText.js'
5
+
6
+ describe('isHtmlFragment', () => {
7
+ it('recognizes the marks TipTap actually emits', () => {
8
+ assert.equal(isHtmlFragment('<p>hello</p>'), true)
9
+ assert.equal(isHtmlFragment('<strong>bold</strong>'), true)
10
+ assert.equal(isHtmlFragment('<em>italic</em>'), true)
11
+ assert.equal(isHtmlFragment('<u>under</u>'), true)
12
+ assert.equal(isHtmlFragment('<s>strike</s>'), true)
13
+ assert.equal(isHtmlFragment('<ul><li>one</li></ul>'), true)
14
+ assert.equal(isHtmlFragment('<ol><li>one</li></ol>'), true)
15
+ assert.equal(isHtmlFragment('<h2>heading</h2>'), true)
16
+ assert.equal(isHtmlFragment('<br>'), true)
17
+ })
18
+
19
+ it('recognizes markup carrying attributes', () => {
20
+ assert.equal(isHtmlFragment('<p style="text-align: center">x</p>'), true)
21
+ assert.equal(isHtmlFragment('<span class="a">x</span>'), true)
22
+ })
23
+
24
+ it('ignores leading whitespace before the first tag', () => {
25
+ assert.equal(isHtmlFragment('\n <p>hello</p>'), true)
26
+ })
27
+
28
+ it('rejects plain text, including text that merely mentions brackets', () => {
29
+ assert.equal(isHtmlFragment('just text'), false)
30
+ assert.equal(isHtmlFragment('5 < 7 and 9 > 2'), false)
31
+ assert.equal(isHtmlFragment('a<b'), false)
32
+ assert.equal(isHtmlFragment(''), false)
33
+ assert.equal(isHtmlFragment(' '), false)
34
+ })
35
+
36
+ it('rejects RTF-conversion artifacts that look bracket-ish', () => {
37
+ // Real values seen in RTF-converted documents; escapeNonTemplateXml exists because of these.
38
+ assert.equal(isHtmlFragment('<å711,0,0>'), false)
39
+ assert.equal(isHtmlFragment('<Pasientnavn>'), false)
40
+ assert.equal(isHtmlFragment('<Some Tag With Spaces>'), false)
41
+ })
42
+
43
+ it('rejects non-string and empty inputs', () => {
44
+ assert.equal(isHtmlFragment(null), false)
45
+ assert.equal(isHtmlFragment(undefined), false)
46
+ })
47
+
48
+ it('detects markup that does not start at the first character', () => {
49
+ assert.equal(isHtmlFragment('Intro text <strong>then bold</strong>'), true)
50
+ })
51
+
52
+ it('recognizes table markup, which pasted content can carry', () => {
53
+ assert.equal(isHtmlFragment('<table><tr><td>a</td></tr></table>'), true)
54
+ })
55
+ })
56
+
57
+ describe('htmlToPlainText', () => {
58
+ it('returns non-HTML input byte-identical', () => {
59
+ assert.equal(htmlToPlainText('just text'), 'just text')
60
+ assert.equal(htmlToPlainText('5 < 7'), '5 < 7')
61
+ assert.equal(htmlToPlainText(' spaced '), ' spaced ')
62
+ assert.equal(htmlToPlainText('line one\nline two'), 'line one\nline two')
63
+ })
64
+
65
+ it('returns an empty string for nullish input', () => {
66
+ assert.equal(htmlToPlainText(null), '')
67
+ assert.equal(htmlToPlainText(undefined), '')
68
+ })
69
+
70
+ it('drops inline marks and keeps the text', () => {
71
+ assert.equal(htmlToPlainText('<p><strong>bold</strong> and <em>italic</em></p>'), 'bold and italic')
72
+ })
73
+
74
+ it('turns paragraphs into newline-separated lines', () => {
75
+ assert.equal(htmlToPlainText('<p>one</p><p>two</p>'), 'one\ntwo')
76
+ })
77
+
78
+ it('turns line breaks into newlines', () => {
79
+ assert.equal(htmlToPlainText('<p>one<br>two</p>'), 'one\ntwo')
80
+ assert.equal(htmlToPlainText('<p>one<br />two</p>'), 'one\ntwo')
81
+ })
82
+
83
+ it('prefixes list items with a dash', () => {
84
+ assert.equal(htmlToPlainText('<ul><li>one</li><li>two</li></ul>'), '- one\n- two')
85
+ })
86
+
87
+ it('keeps headings as their own lines', () => {
88
+ assert.equal(htmlToPlainText('<h1>Title</h1><p>body</p>'), 'Title\nbody')
89
+ })
90
+
91
+ it('decodes the entities TipTap emits', () => {
92
+ assert.equal(htmlToPlainText('<p>a&nbsp;b</p>'), 'a b')
93
+ assert.equal(htmlToPlainText('<p>&lt;tag&gt;</p>'), '<tag>')
94
+ assert.equal(htmlToPlainText('<p>a&amp;b</p>'), 'a&b')
95
+ assert.equal(htmlToPlainText('<p>&quot;q&quot; &#39;a&#39;</p>'), '"q" \'a\'')
96
+ })
97
+
98
+ it('collapses the blank lines empty paragraphs would leave', () => {
99
+ assert.equal(htmlToPlainText('<p>one</p><p></p><p>two</p>'), 'one\ntwo')
100
+ })
101
+
102
+ it('degrades malformed markup to text rather than leaking tags', () => {
103
+ assert.equal(htmlToPlainText('<p>unclosed'), 'unclosed')
104
+ assert.equal(htmlToPlainText('<p>a</p><p>b'), 'a\nb')
105
+ })
106
+
107
+ it('keeps table cells apart instead of running them together', () => {
108
+ assert.equal(htmlToPlainText('<table><tr><td>a</td><td>b</td></tr><tr><td>c</td></tr></table>'), 'a\tb\nc')
109
+ })
110
+
111
+ it('never leaves an angle bracket from a real tag behind', () => {
112
+ const out = htmlToPlainText('<div><p>x</p><ul><li>y</li></ul></div>')
113
+ assert.equal(out.includes('<'), false)
114
+ assert.equal(out.includes('>'), false)
115
+ })
116
+ })
@@ -0,0 +1,109 @@
1
+ /**
2
+ * Rich-text values (TipTap output) are stored as HTML strings. Consumers that are not rich-text
3
+ * interpreters — plain inputs, titles, document merge fields — need to tell markup from ordinary
4
+ * text and render it readably. These helpers are the single implementation of both.
5
+ *
6
+ * No DOM: this runs in the browser apps and in the Bun backend that generates documents.
7
+ */
8
+
9
+ /** The subset TipTap's StarterKit emits, plus the containers it wraps them in. */
10
+ const KNOWN_TAGS = [
11
+ 'p',
12
+ 'br',
13
+ 'div',
14
+ 'span',
15
+ 'b',
16
+ 'strong',
17
+ 'i',
18
+ 'em',
19
+ 'u',
20
+ 's',
21
+ 'strike',
22
+ 'del',
23
+ 'ul',
24
+ 'ol',
25
+ 'li',
26
+ 'h1',
27
+ 'h2',
28
+ 'h3',
29
+ 'h4',
30
+ 'h5',
31
+ 'h6',
32
+ 'blockquote',
33
+ 'pre',
34
+ 'code',
35
+ 'a',
36
+ // Not emitted by StarterKit, but pasted content and legacy values carry tables; without them
37
+ // a table would fail the sniff and reach the user as visible markup.
38
+ 'table',
39
+ 'thead',
40
+ 'tbody',
41
+ 'tr',
42
+ 'td',
43
+ 'th',
44
+ ] as const
45
+
46
+ /**
47
+ * Matches an opening/closing/self-closing tag from KNOWN_TAGS only.
48
+ *
49
+ * Deliberately narrow: legacy values carry RTF-conversion artifacts (`<å711,0,0>`) and document
50
+ * merge placeholders (`<Pasientnavn>`) that are bracket-shaped but must never be treated as markup.
51
+ */
52
+ const KNOWN_TAG_PATTERN = new RegExp(String.raw`<\s*/?\s*(?:${KNOWN_TAGS.join('|')})(?:\s[^<>]*)?/?\s*>`, 'i')
53
+
54
+ /** Any tag-shaped run, used for stripping once a value is known to be markup. */
55
+ const ANY_TAG_PATTERN = /<[^<>]*>/g
56
+
57
+ const BLOCK_BOUNDARY_PATTERN = /<\s*\/?\s*(?:p|div|h[1-6]|blockquote|pre|ul|ol|tr)(?:\s[^<>]*)?\/?\s*>/gi
58
+ const LINE_BREAK_PATTERN = /<\s*br(?:\s[^<>]*)?\/?\s*>/gi
59
+ const LIST_ITEM_PATTERN = /<\s*li(?:\s[^<>]*)?\s*>/gi
60
+ /** Cells are separated rather than newline-split, so a row stays one line. */
61
+ const TABLE_CELL_PATTERN = /<\s*\/\s*(?:td|th)\s*>(?=\s*<\s*(?:td|th)[\s>])/gi
62
+
63
+ const ENTITIES: ReadonlyArray<readonly [RegExp, string]> = [
64
+ [/&nbsp;/gi, ' '],
65
+ [/&lt;/gi, '<'],
66
+ [/&gt;/gi, '>'],
67
+ [/&quot;/gi, '"'],
68
+ [/&#0*39;|&apos;/gi, "'"],
69
+ // Ampersand last, so `&amp;lt;` decodes to `&lt;` rather than to `<`.
70
+ [/&amp;/gi, '&'],
71
+ ]
72
+
73
+ const decodeEntities = (value: string): string =>
74
+ ENTITIES.reduce((acc, [pattern, replacement]) => acc.replace(pattern, replacement), value)
75
+
76
+ /**
77
+ * Whether a value should be treated as an HTML fragment.
78
+ *
79
+ * Conservative by design — a false positive corrupts a legitimate plain value, while a false
80
+ * negative only leaves today's behaviour unchanged.
81
+ */
82
+ export function isHtmlFragment(value: string | null | undefined): boolean {
83
+ if (typeof value !== 'string' || value.length === 0) return false
84
+
85
+ return KNOWN_TAG_PATTERN.test(value)
86
+ }
87
+
88
+ /**
89
+ * Renders an HTML fragment as readable plain text, preserving structure: blocks and `<br>` become
90
+ * newlines and list items gain a leading dash.
91
+ *
92
+ * Values that are not markup are returned **byte-identical** — callers may pass anything.
93
+ */
94
+ export function htmlToPlainText(value: string | null | undefined): string {
95
+ if (typeof value !== 'string' || value.length === 0) return ''
96
+ if (!isHtmlFragment(value)) return value
97
+
98
+ const withStructure = value
99
+ .replace(LINE_BREAK_PATTERN, '\n')
100
+ .replace(LIST_ITEM_PATTERN, '\n- ')
101
+ .replace(TABLE_CELL_PATTERN, '\t')
102
+ .replace(BLOCK_BOUNDARY_PATTERN, '\n')
103
+
104
+ return decodeEntities(withStructure.replace(ANY_TAG_PATTERN, ''))
105
+ .split('\n')
106
+ .map((line) => line.trim())
107
+ .filter((line) => line.length > 0 && line !== '-')
108
+ .join('\n')
109
+ }
package/src/index.ts CHANGED
@@ -5,6 +5,7 @@ export * from './helpers/CapitalizeFistLetter.js'
5
5
  export * from './helpers/IsAdcuris.js'
6
6
  export * from './helpers/IsAdopus.js'
7
7
  export * from './helpers/IsNotEmpty.js'
8
+ export * from './helpers/htmlText.js'
8
9
  export * from './helpers/Config.js'
9
10
  export * from './helpers/InitializeIDBListenersOnMstSnapshots.js'
10
11
  export * from './helpers/initEnvConfigsVars.js'