@chayns-components/format 5.0.0-beta.1007

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.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -0
  3. package/lib/cjs/constants/format.js +8 -0
  4. package/lib/cjs/constants/format.js.map +1 -0
  5. package/lib/cjs/index.js +20 -0
  6. package/lib/cjs/index.js.map +1 -0
  7. package/lib/cjs/types/format.js +2 -0
  8. package/lib/cjs/types/format.js.map +1 -0
  9. package/lib/cjs/utils/escape.js +9 -0
  10. package/lib/cjs/utils/escape.js.map +1 -0
  11. package/lib/cjs/utils/formatString/bb-code/findBBCode.js +45 -0
  12. package/lib/cjs/utils/formatString/bb-code/findBBCode.js.map +1 -0
  13. package/lib/cjs/utils/formatString/bb-code/formatBBCode.js +94 -0
  14. package/lib/cjs/utils/formatString/bb-code/formatBBCode.js.map +1 -0
  15. package/lib/cjs/utils/formatString/formatString.js +83 -0
  16. package/lib/cjs/utils/formatString/formatString.js.map +1 -0
  17. package/lib/cjs/utils/formatString/formatString.test.js +554 -0
  18. package/lib/cjs/utils/formatString/formatString.test.js.map +1 -0
  19. package/lib/cjs/utils/formatString/markdown/formatMarkdown.js +173 -0
  20. package/lib/cjs/utils/formatString/markdown/formatMarkdown.js.map +1 -0
  21. package/lib/esm/constants/format.js +2 -0
  22. package/lib/esm/constants/format.js.map +1 -0
  23. package/lib/esm/index.js +3 -0
  24. package/lib/esm/index.js.map +1 -0
  25. package/lib/esm/types/format.js +2 -0
  26. package/lib/esm/types/format.js.map +1 -0
  27. package/lib/esm/utils/escape.js +2 -0
  28. package/lib/esm/utils/escape.js.map +1 -0
  29. package/lib/esm/utils/formatString/bb-code/findBBCode.js +39 -0
  30. package/lib/esm/utils/formatString/bb-code/findBBCode.js.map +1 -0
  31. package/lib/esm/utils/formatString/bb-code/formatBBCode.js +88 -0
  32. package/lib/esm/utils/formatString/bb-code/formatBBCode.js.map +1 -0
  33. package/lib/esm/utils/formatString/formatString.js +76 -0
  34. package/lib/esm/utils/formatString/formatString.js.map +1 -0
  35. package/lib/esm/utils/formatString/formatString.test.js +552 -0
  36. package/lib/esm/utils/formatString/formatString.test.js.map +1 -0
  37. package/lib/esm/utils/formatString/markdown/formatMarkdown.js +163 -0
  38. package/lib/esm/utils/formatString/markdown/formatMarkdown.js.map +1 -0
  39. package/lib/types/constants/format.d.ts +1 -0
  40. package/lib/types/index.d.ts +3 -0
  41. package/lib/types/types/format.d.ts +5 -0
  42. package/lib/types/utils/escape.d.ts +1 -0
  43. package/lib/types/utils/formatString/bb-code/findBBCode.d.ts +10 -0
  44. package/lib/types/utils/formatString/bb-code/formatBBCode.d.ts +11 -0
  45. package/lib/types/utils/formatString/formatString.d.ts +12 -0
  46. package/lib/types/utils/formatString/formatString.test.d.ts +1 -0
  47. package/lib/types/utils/formatString/markdown/formatMarkdown.d.ts +3 -0
  48. package/package.json +78 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Tobit Laboratories AG
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # `format`
2
+
3
+ > TODO: description
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ const format = require('format');
9
+
10
+ // TODO: DEMONSTRATE API
11
+ ```
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.BB_CODE_HTML_TAG_PREFIX = void 0;
7
+ const BB_CODE_HTML_TAG_PREFIX = exports.BB_CODE_HTML_TAG_PREFIX = 'bb-code-';
8
+ //# sourceMappingURL=format.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format.js","names":["BB_CODE_HTML_TAG_PREFIX","exports"],"sources":["../../../src/constants/format.ts"],"sourcesContent":["export const BB_CODE_HTML_TAG_PREFIX = 'bb-code-';\n"],"mappings":";;;;;;AAAO,MAAMA,uBAAuB,GAAAC,OAAA,CAAAD,uBAAA,GAAG,UAAU","ignoreList":[]}
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "BB_CODE_HTML_TAG_PREFIX", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _format.BB_CODE_HTML_TAG_PREFIX;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "formatStringToHtml", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _formatString.formatStringToHtml;
16
+ }
17
+ });
18
+ var _format = require("./constants/format");
19
+ var _formatString = require("./utils/formatString/formatString");
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["_format","require","_formatString"],"sources":["../../src/index.ts"],"sourcesContent":["export { BB_CODE_HTML_TAG_PREFIX } from './constants/format';\nexport type { TableObject } from './types/format';\nexport { formatStringToHtml } from './utils/formatString/formatString';\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA,IAAAA,OAAA,GAAAC,OAAA;AAEA,IAAAC,aAAA,GAAAD,OAAA","ignoreList":[]}
@@ -0,0 +1,2 @@
1
+
2
+ //# sourceMappingURL=format.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format.js","names":[],"sources":["../../../src/types/format.ts"],"sourcesContent":["export type TableObject = {\n raw: string;\n csv: string;\n id: string;\n};\n"],"mappings":"","ignoreList":[]}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.escapeHtmlInText = void 0;
7
+ const escapeHtmlInText = text => text.replace(/</g, '&lt;').replace(/>/g, '&gt;');
8
+ exports.escapeHtmlInText = escapeHtmlInText;
9
+ //# sourceMappingURL=escape.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escape.js","names":["escapeHtmlInText","text","replace","exports"],"sources":["../../../src/utils/escape.ts"],"sourcesContent":["export const escapeHtmlInText = (text: string): string =>\n text.replace(/</g, '&lt;').replace(/>/g, '&gt;');\n"],"mappings":";;;;;;AAAO,MAAMA,gBAAgB,GAAIC,IAAY,IACzCA,IAAI,CAACC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAACA,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;AAACC,OAAA,CAAAH,gBAAA,GAAAA,gBAAA","ignoreList":[]}
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.findFirstBBCode = findFirstBBCode;
7
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
8
+ // @ts-ignore
9
+ const BB_REGEX = /(\[([a-zA-Z0-9_]*)(.*?)\])(.*?)(\[\/\2\])/s;
10
+ // Also matches "\" before quote to fix button for voucher messages
11
+ const PARAMETER_REGEX = /([\w]*?)=\\?["„“‚'‘](.*?)["“”‘'’]/g;
12
+ // TODO Use external package instead of RegExp to parse BBCode.
13
+ // It is not recommended to use RegExp to parse markup languages like BBCode, because it is not possible to parse nested tags with it.
14
+
15
+ // Finds and returns the first BBCode tag in the input string.
16
+ function findFirstBBCode(inputString) {
17
+ const matches = BB_REGEX.exec(inputString);
18
+ if (matches !== null) {
19
+ const [fullMatch, openingTag, tag, params, content, closingTag] = matches;
20
+ const {
21
+ index
22
+ } = matches;
23
+ if (fullMatch === undefined || tag === undefined || params === undefined || content === undefined || openingTag === undefined || closingTag === undefined) return null;
24
+ const parameters = {};
25
+ let match = null;
26
+ // eslint-disable-next-line no-cond-assign
27
+ while (match = PARAMETER_REGEX.exec(params)) {
28
+ const [, key, value] = match;
29
+ if (value) {
30
+ parameters[key || 'url'] = value;
31
+ }
32
+ }
33
+ return {
34
+ fullMatch,
35
+ tag,
36
+ parameters,
37
+ content,
38
+ index,
39
+ openingTag,
40
+ closingTag
41
+ };
42
+ }
43
+ return null;
44
+ }
45
+ //# sourceMappingURL=findBBCode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"findBBCode.js","names":["BB_REGEX","PARAMETER_REGEX","findFirstBBCode","inputString","matches","exec","fullMatch","openingTag","tag","params","content","closingTag","index","undefined","parameters","match","key","value"],"sources":["../../../../../src/utils/formatString/bb-code/findBBCode.ts"],"sourcesContent":["// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-ignore\nconst BB_REGEX = /(\\[([a-zA-Z0-9_]*)(.*?)\\])(.*?)(\\[\\/\\2\\])/s;\n// Also matches \"\\\" before quote to fix button for voucher messages\nconst PARAMETER_REGEX = /([\\w]*?)=\\\\?[\"„“‚'‘](.*?)[\"“”‘'’]/g;\n\nexport interface BBCodeMatch {\n fullMatch: string;\n tag: string;\n parameters: Record<string, string>;\n content: string;\n index: number;\n openingTag: string;\n closingTag: string;\n}\n\n// TODO Use external package instead of RegExp to parse BBCode.\n// It is not recommended to use RegExp to parse markup languages like BBCode, because it is not possible to parse nested tags with it.\n\n// Finds and returns the first BBCode tag in the input string.\nexport function findFirstBBCode(inputString: string): BBCodeMatch | null {\n const matches = BB_REGEX.exec(inputString);\n\n if (matches !== null) {\n const [fullMatch, openingTag, tag, params, content, closingTag] = matches;\n const { index } = matches;\n\n if (\n fullMatch === undefined ||\n tag === undefined ||\n params === undefined ||\n content === undefined ||\n openingTag === undefined ||\n closingTag === undefined\n )\n return null;\n\n const parameters: Record<string, string> = {};\n\n let match: RegExpExecArray | null = null;\n // eslint-disable-next-line no-cond-assign\n while ((match = PARAMETER_REGEX.exec(params))) {\n const [, key, value] = match;\n\n if (value) {\n parameters[key || 'url'] = value;\n }\n }\n\n return {\n fullMatch,\n tag,\n parameters,\n content,\n index,\n openingTag,\n closingTag,\n };\n }\n\n return null;\n}\n"],"mappings":";;;;;;AAAA;AACA;AACA,MAAMA,QAAQ,GAAG,4CAA4C;AAC7D;AACA,MAAMC,eAAe,GAAG,oCAAoC;AAY5D;AACA;;AAEA;AACO,SAASC,eAAeA,CAACC,WAAmB,EAAsB;EACrE,MAAMC,OAAO,GAAGJ,QAAQ,CAACK,IAAI,CAACF,WAAW,CAAC;EAE1C,IAAIC,OAAO,KAAK,IAAI,EAAE;IAClB,MAAM,CAACE,SAAS,EAAEC,UAAU,EAAEC,GAAG,EAAEC,MAAM,EAAEC,OAAO,EAAEC,UAAU,CAAC,GAAGP,OAAO;IACzE,MAAM;MAAEQ;IAAM,CAAC,GAAGR,OAAO;IAEzB,IACIE,SAAS,KAAKO,SAAS,IACvBL,GAAG,KAAKK,SAAS,IACjBJ,MAAM,KAAKI,SAAS,IACpBH,OAAO,KAAKG,SAAS,IACrBN,UAAU,KAAKM,SAAS,IACxBF,UAAU,KAAKE,SAAS,EAExB,OAAO,IAAI;IAEf,MAAMC,UAAkC,GAAG,CAAC,CAAC;IAE7C,IAAIC,KAA6B,GAAG,IAAI;IACxC;IACA,OAAQA,KAAK,GAAGd,eAAe,CAACI,IAAI,CAACI,MAAM,CAAC,EAAG;MAC3C,MAAM,GAAGO,GAAG,EAAEC,KAAK,CAAC,GAAGF,KAAK;MAE5B,IAAIE,KAAK,EAAE;QACPH,UAAU,CAACE,GAAG,IAAI,KAAK,CAAC,GAAGC,KAAK;MACpC;IACJ;IAEA,OAAO;MACHX,SAAS;MACTE,GAAG;MACHM,UAAU;MACVJ,OAAO;MACPE,KAAK;MACLL,UAAU;MACVI;IACJ,CAAC;EACL;EAEA,OAAO,IAAI;AACf","ignoreList":[]}
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.unescapeBBCodeSquareBrackets = exports.parseBBCode = exports.escapeBBCodeSquareBrackets = void 0;
7
+ var _format = require("../../../constants/format");
8
+ var _findBBCode = require("./findBBCode");
9
+ const BLOCK_LEVEL_TAGS = ['center', 'ul', 'ol', 'li', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p'];
10
+ const INLINE_LEVEL_TAGS = ['b', 'strong', 'i', 'em', 'u', 's', 'span', 'img'];
11
+ // Parses BB-Code to HTML recursively.
12
+ // When justEscapeSquareBrackets is true, square brackets are escaped to prevent conflicts between markdown and BB Code.
13
+ // In that case the function only escapes square brackets and doesn't remove line breaks.
14
+ const parseBBCode = (text, options) => {
15
+ const {
16
+ customBlockLevelBBCodeTags: customBlockLevelTags = [],
17
+ customInlineLevelBBCodeTags: customInlineLevelTags = [],
18
+ justEscapeSquareBrackets = false
19
+ } = options || {};
20
+ let html = text;
21
+
22
+ // This index is used to keep track of the position in the html string that is being parsed.
23
+ let parseBehindIndex = 0;
24
+ while (parseBehindIndex < html.length) {
25
+ const htmlToParse = html.slice(parseBehindIndex);
26
+ const firstBBCodeMatch = (0, _findBBCode.findFirstBBCode)(htmlToParse);
27
+
28
+ // Stops parsing if no BB-Code is found.
29
+ if (!firstBBCodeMatch) {
30
+ return html;
31
+ }
32
+ const {
33
+ content,
34
+ fullMatch,
35
+ parameters,
36
+ index,
37
+ openingTag,
38
+ closingTag
39
+ } = firstBBCodeMatch;
40
+ const Tag = firstBBCodeMatch.tag.toLowerCase();
41
+ const isValidTag = [...BLOCK_LEVEL_TAGS, ...customBlockLevelTags, ...INLINE_LEVEL_TAGS, ...customInlineLevelTags].includes(Tag);
42
+ const isBlockLevelTag = [...BLOCK_LEVEL_TAGS, ...customBlockLevelTags].includes(Tag);
43
+
44
+ // Ignores tags that are not supported.
45
+ if (!isValidTag) {
46
+ // The parsing continues behind the first square bracket of the BB-Code tag.
47
+ parseBehindIndex += index + 1;
48
+ // eslint-disable-next-line no-continue
49
+ continue;
50
+ }
51
+
52
+ // Converts BB-Code tag's content before converting itself, because it may contain other BB-Codes.
53
+ let parsedContent = parseBBCode(content, options);
54
+ if (justEscapeSquareBrackets) {
55
+ const indexOfFullMatch = html.indexOf(fullMatch);
56
+ const escapedOpeningTag = escapeBBCodeSquareBrackets(openingTag);
57
+ const escapedClosingTag = escapeBBCodeSquareBrackets(closingTag);
58
+
59
+ // Removes leading and trailing line-breaks from within bb code elements, to prevent unwanted spacing.
60
+ // This needs to be done before formatting Markdown, so the Markdown formatting doesn't interpret line breaks unexpectedly.
61
+ parsedContent = parsedContent.replace(/^\n+|\n+$/g, '');
62
+
63
+ // Simply escapes the square brackets of the BB-Code opening and closing tag.
64
+ html = html.slice(0, indexOfFullMatch) + escapedOpeningTag + parsedContent + escapedClosingTag + html.slice(indexOfFullMatch + fullMatch.length);
65
+
66
+ // Continues parsing behind the parsed bb-code.
67
+ parseBehindIndex = indexOfFullMatch + escapedOpeningTag.length + parsedContent.length + escapedClosingTag.length;
68
+ } else {
69
+ const indexOfFullMatch = html.indexOf(fullMatch);
70
+ let htmlAfterTag = html.slice(indexOfFullMatch + fullMatch.length);
71
+
72
+ // Removes leading line-break (ONE, NOT ALL) after block level elements, to prevent unwanted spacing.
73
+ if (isBlockLevelTag) {
74
+ htmlAfterTag = htmlAfterTag.replace(/^\n/, '');
75
+ }
76
+ const isCustomTag = [...customBlockLevelTags, ...customInlineLevelTags].includes(Tag);
77
+ const htmlTag = isCustomTag ? `${_format.BB_CODE_HTML_TAG_PREFIX}${Tag}` : Tag;
78
+ const openingHtmlTag = `<${htmlTag}${Object.entries(parameters).length > 0 ? ' ' : ''}${Object.entries(parameters).map(([key, value]) => `${key}="${value}"`).join(' ')}>`;
79
+ const closingHtmlTag = `</${htmlTag}>`;
80
+ const element = Tag === 'img' ? openingHtmlTag : openingHtmlTag + parsedContent + closingHtmlTag;
81
+ html = `${html.slice(0, indexOfFullMatch)}${element}${htmlAfterTag}`;
82
+
83
+ // Continues parsing behind the parsed bb-code.
84
+ parseBehindIndex = indexOfFullMatch + element.length;
85
+ }
86
+ }
87
+ return html;
88
+ };
89
+ exports.parseBBCode = parseBBCode;
90
+ const escapeBBCodeSquareBrackets = text => text.replaceAll('[', '&zwj;[&zwj;').replaceAll(']', '&zwj;]&zwj;');
91
+ exports.escapeBBCodeSquareBrackets = escapeBBCodeSquareBrackets;
92
+ const unescapeBBCodeSquareBrackets = text => text.replaceAll('&zwj;[&zwj;', '[').replaceAll('&zwj;]&zwj;', ']');
93
+ exports.unescapeBBCodeSquareBrackets = unescapeBBCodeSquareBrackets;
94
+ //# sourceMappingURL=formatBBCode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatBBCode.js","names":["_format","require","_findBBCode","BLOCK_LEVEL_TAGS","INLINE_LEVEL_TAGS","parseBBCode","text","options","customBlockLevelBBCodeTags","customBlockLevelTags","customInlineLevelBBCodeTags","customInlineLevelTags","justEscapeSquareBrackets","html","parseBehindIndex","length","htmlToParse","slice","firstBBCodeMatch","findFirstBBCode","content","fullMatch","parameters","index","openingTag","closingTag","Tag","tag","toLowerCase","isValidTag","includes","isBlockLevelTag","parsedContent","indexOfFullMatch","indexOf","escapedOpeningTag","escapeBBCodeSquareBrackets","escapedClosingTag","replace","htmlAfterTag","isCustomTag","htmlTag","BB_CODE_HTML_TAG_PREFIX","openingHtmlTag","Object","entries","map","key","value","join","closingHtmlTag","element","exports","replaceAll","unescapeBBCodeSquareBrackets"],"sources":["../../../../../src/utils/formatString/bb-code/formatBBCode.ts"],"sourcesContent":["import { BB_CODE_HTML_TAG_PREFIX } from '../../../constants/format';\nimport { findFirstBBCode } from './findBBCode';\n\nconst BLOCK_LEVEL_TAGS = ['center', 'ul', 'ol', 'li', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p'];\nconst INLINE_LEVEL_TAGS = ['b', 'strong', 'i', 'em', 'u', 's', 'span', 'img'];\n\nexport interface ParseBBCodesOptions {\n customBlockLevelBBCodeTags?: string[];\n customInlineLevelBBCodeTags?: string[];\n}\n\ninterface ParseBBCodePrivateOptions extends ParseBBCodesOptions {\n justEscapeSquareBrackets?: boolean;\n}\n\n// Parses BB-Code to HTML recursively.\n// When justEscapeSquareBrackets is true, square brackets are escaped to prevent conflicts between markdown and BB Code.\n// In that case the function only escapes square brackets and doesn't remove line breaks.\nexport const parseBBCode = (text: string, options?: ParseBBCodePrivateOptions) => {\n const {\n customBlockLevelBBCodeTags: customBlockLevelTags = [],\n customInlineLevelBBCodeTags: customInlineLevelTags = [],\n justEscapeSquareBrackets = false,\n } = options || {};\n\n let html = text;\n\n // This index is used to keep track of the position in the html string that is being parsed.\n let parseBehindIndex = 0;\n\n while (parseBehindIndex < html.length) {\n const htmlToParse = html.slice(parseBehindIndex);\n\n const firstBBCodeMatch = findFirstBBCode(htmlToParse);\n\n // Stops parsing if no BB-Code is found.\n if (!firstBBCodeMatch) {\n return html;\n }\n\n const { content, fullMatch, parameters, index, openingTag, closingTag } = firstBBCodeMatch;\n\n const Tag = firstBBCodeMatch.tag.toLowerCase();\n const isValidTag = [\n ...BLOCK_LEVEL_TAGS,\n ...customBlockLevelTags,\n ...INLINE_LEVEL_TAGS,\n ...customInlineLevelTags,\n ].includes(Tag);\n const isBlockLevelTag = [...BLOCK_LEVEL_TAGS, ...customBlockLevelTags].includes(Tag);\n\n // Ignores tags that are not supported.\n if (!isValidTag) {\n // The parsing continues behind the first square bracket of the BB-Code tag.\n parseBehindIndex += index + 1;\n // eslint-disable-next-line no-continue\n continue;\n }\n\n // Converts BB-Code tag's content before converting itself, because it may contain other BB-Codes.\n let parsedContent = parseBBCode(content, options);\n\n if (justEscapeSquareBrackets) {\n const indexOfFullMatch = html.indexOf(fullMatch);\n const escapedOpeningTag = escapeBBCodeSquareBrackets(openingTag);\n const escapedClosingTag = escapeBBCodeSquareBrackets(closingTag);\n\n // Removes leading and trailing line-breaks from within bb code elements, to prevent unwanted spacing.\n // This needs to be done before formatting Markdown, so the Markdown formatting doesn't interpret line breaks unexpectedly.\n parsedContent = parsedContent.replace(/^\\n+|\\n+$/g, '');\n\n // Simply escapes the square brackets of the BB-Code opening and closing tag.\n html =\n html.slice(0, indexOfFullMatch) +\n escapedOpeningTag +\n parsedContent +\n escapedClosingTag +\n html.slice(indexOfFullMatch + fullMatch.length);\n\n // Continues parsing behind the parsed bb-code.\n parseBehindIndex =\n indexOfFullMatch +\n escapedOpeningTag.length +\n parsedContent.length +\n escapedClosingTag.length;\n } else {\n const indexOfFullMatch = html.indexOf(fullMatch);\n\n let htmlAfterTag = html.slice(indexOfFullMatch + fullMatch.length);\n\n // Removes leading line-break (ONE, NOT ALL) after block level elements, to prevent unwanted spacing.\n if (isBlockLevelTag) {\n htmlAfterTag = htmlAfterTag.replace(/^\\n/, '');\n }\n\n const isCustomTag = [...customBlockLevelTags, ...customInlineLevelTags].includes(Tag);\n const htmlTag = isCustomTag ? `${BB_CODE_HTML_TAG_PREFIX}${Tag}` : Tag;\n const openingHtmlTag = `<${htmlTag}${Object.entries(parameters).length > 0 ? ' ' : ''}${Object.entries(\n parameters,\n )\n .map(([key, value]) => `${key}=\"${value}\"`)\n .join(' ')}>`;\n const closingHtmlTag = `</${htmlTag}>`;\n\n const element =\n Tag === 'img' ? openingHtmlTag : openingHtmlTag + parsedContent + closingHtmlTag;\n\n html = `${html.slice(0, indexOfFullMatch)}${element}${htmlAfterTag}`;\n\n // Continues parsing behind the parsed bb-code.\n parseBehindIndex = indexOfFullMatch + element.length;\n }\n }\n\n return html;\n};\n\nexport const escapeBBCodeSquareBrackets = (text: string) =>\n text.replaceAll('[', '&zwj;[&zwj;').replaceAll(']', '&zwj;]&zwj;');\n\nexport const unescapeBBCodeSquareBrackets = (text: string) =>\n text.replaceAll('&zwj;[&zwj;', '[').replaceAll('&zwj;]&zwj;', ']');\n"],"mappings":";;;;;;AAAA,IAAAA,OAAA,GAAAC,OAAA;AACA,IAAAC,WAAA,GAAAD,OAAA;AAEA,MAAME,gBAAgB,GAAG,CAAC,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,CAAC;AAC9F,MAAMC,iBAAiB,GAAG,CAAC,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC;AAW7E;AACA;AACA;AACO,MAAMC,WAAW,GAAGA,CAACC,IAAY,EAAEC,OAAmC,KAAK;EAC9E,MAAM;IACFC,0BAA0B,EAAEC,oBAAoB,GAAG,EAAE;IACrDC,2BAA2B,EAAEC,qBAAqB,GAAG,EAAE;IACvDC,wBAAwB,GAAG;EAC/B,CAAC,GAAGL,OAAO,IAAI,CAAC,CAAC;EAEjB,IAAIM,IAAI,GAAGP,IAAI;;EAEf;EACA,IAAIQ,gBAAgB,GAAG,CAAC;EAExB,OAAOA,gBAAgB,GAAGD,IAAI,CAACE,MAAM,EAAE;IACnC,MAAMC,WAAW,GAAGH,IAAI,CAACI,KAAK,CAACH,gBAAgB,CAAC;IAEhD,MAAMI,gBAAgB,GAAG,IAAAC,2BAAe,EAACH,WAAW,CAAC;;IAErD;IACA,IAAI,CAACE,gBAAgB,EAAE;MACnB,OAAOL,IAAI;IACf;IAEA,MAAM;MAAEO,OAAO;MAAEC,SA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IAAI,CAAC+C,UAAU,CAAC,GAAG,EAAE,aAAa,CAAC,CAACA,UAAU,CAAC,GAAG,EAAE,aAAa,CAAC;AAACD,OAAA,CAAAhB,0BAAA,GAAAA,0BAAA;AAEhE,MAAMkB,4BAA4B,GAAIhD,IAAY,IACrDA,IAAI,CAAC+C,UAAU,CAAC,aAAa,EAAE,GAAG,CAAC,CAACA,UAAU,CAAC,aAAa,EAAE,GAAG,CAAC;AAACD,OAAA,CAAAE,4BAAA,GAAAA,4BAAA","ignoreList":[]}
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.formatStringToHtml = void 0;
7
+ var _escape = require("../escape");
8
+ var _formatBBCode = require("./bb-code/formatBBCode");
9
+ var _formatMarkdown = require("./markdown/formatMarkdown");
10
+ // This function takes a string and returns formatted html as a string.
11
+ const formatStringToHtml = (string, options) => {
12
+ if (!string) {
13
+ return {
14
+ html: '',
15
+ tables: []
16
+ };
17
+ }
18
+ const {
19
+ parseMarkdown: parseMarkdownOption = true,
20
+ parseBBCode: parseBBCodeOption = false,
21
+ customInlineLevelBBCodeTags = [],
22
+ customBlockLevelBBCodeTags = []
23
+ } = options || {};
24
+ let formattedString = string;
25
+
26
+ // Needs to get the tables before escaping html and parsing bb-code, so the original content can be extracted.
27
+ const tables = [];
28
+ if (parseMarkdownOption) {
29
+ try {
30
+ tables.push(...(0, _formatMarkdown.getMarkdownTables)(formattedString));
31
+ } catch (error) {
32
+ console.warn('[@chayns-components/format] Warning: Failed to get markdown tables', error);
33
+ }
34
+ }
35
+
36
+ // Escape HTML entities.
37
+ formattedString = (0, _escape.escapeHtmlInText)(formattedString);
38
+
39
+ // Escape BB-Code, to prevent conflicts between markdown and bb-code. Specifically [b]test[/b]() would be a problem, since markdown interprets parts of this as a link.
40
+
41
+ // Parses markdown to HTML.
42
+ if (parseMarkdownOption) {
43
+ try {
44
+ if (parseBBCodeOption) {
45
+ // Escapes BB-Code brackets.
46
+ formattedString = (0, _formatBBCode.parseBBCode)(formattedString, {
47
+ customInlineLevelBBCodeTags,
48
+ customBlockLevelBBCodeTags,
49
+ justEscapeSquareBrackets: true
50
+ });
51
+ }
52
+ formattedString = (0, _formatMarkdown.parseMarkdown)(formattedString, parseBBCodeOption);
53
+
54
+ // Remove trailing \n
55
+ formattedString = formattedString.replace(/\n$/, '');
56
+ if (parseBBCodeOption) {
57
+ // Unescapes BB-Code brackets.
58
+ formattedString = (0, _formatBBCode.unescapeBBCodeSquareBrackets)(formattedString);
59
+ }
60
+ } catch (error) {
61
+ console.warn('[@chayns-components/format] Warning: Failed to parse markdown', error);
62
+ }
63
+ }
64
+
65
+ // Parses BB-Code to HTML.
66
+ if (parseBBCodeOption) {
67
+ try {
68
+ formattedString = (0, _formatBBCode.parseBBCode)(formattedString, {
69
+ customInlineLevelBBCodeTags,
70
+ customBlockLevelBBCodeTags
71
+ });
72
+ formattedString = (0, _formatBBCode.unescapeBBCodeSquareBrackets)(formattedString);
73
+ } catch (error) {
74
+ console.warn('[@chayns-components/format] Warning: Failed to parse bb-code', error);
75
+ }
76
+ }
77
+ return {
78
+ html: formattedString,
79
+ tables
80
+ };
81
+ };
82
+ exports.formatStringToHtml = formatStringToHtml;
83
+ //# sourceMappingURL=formatString.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatString.js","names":["_escape","require","_formatBBCode","_formatMarkdown","formatStringToHtml","string","options","html","tables","parseMarkdown","parseMarkdownOption","parseBBCode","parseBBCodeOption","customInlineLevelBBCodeTags","customBlockLevelBBCodeTags","formattedString","push","getMarkdownTables","error","console","warn","escapeHtmlInText","justEscapeSquareBrackets","replace","unescapeBBCodeSquareBrackets","exports"],"sources":["../../../../src/utils/formatString/formatString.ts"],"sourcesContent":["import type { TableObject } from '../../types/format';\nimport { escapeHtmlInText } from '../escape';\nimport {\n parseBBCode,\n ParseBBCodesOptions,\n unescapeBBCodeSquareBrackets,\n} from './bb-code/formatBBCode';\nimport { getMarkdownTables, parseMarkdown } from './markdown/formatMarkdown';\n\ninterface FormatStringOptions extends ParseBBCodesOptions {\n parseMarkdown?: boolean;\n parseBBCode?: boolean;\n}\n\ninterface FormatStringResult {\n html: string;\n tables: TableObject[];\n}\n\n// This function takes a string and returns formatted html as a string.\nexport const formatStringToHtml = (\n string: string,\n options?: FormatStringOptions,\n): FormatStringResult => {\n if (!string) {\n return {\n html: '',\n tables: [],\n };\n }\n\n const {\n parseMarkdown: parseMarkdownOption = true,\n parseBBCode: parseBBCodeOption = false,\n customInlineLevelBBCodeTags = [],\n customBlockLevelBBCodeTags = [],\n } = options || {};\n\n let formattedString = string;\n\n // Needs to get the tables before escaping html and parsing bb-code, so the original content can be extracted.\n const tables: TableObject[] = [];\n if (parseMarkdownOption) {\n try {\n tables.push(...getMarkdownTables(formattedString));\n } catch (error) {\n console.warn(\n '[@chayns-components/format] Warning: Failed to get markdown tables',\n error,\n );\n }\n }\n\n // Escape HTML entities.\n formattedString = escapeHtmlInText(formattedString);\n\n // Escape BB-Code, to prevent conflicts between markdown and bb-code. Specifically [b]test[/b]() would be a problem, since markdown interprets parts of this as a link.\n\n // Parses markdown to HTML.\n if (parseMarkdownOption) {\n try {\n if (parseBBCodeOption) {\n // Escapes BB-Code brackets.\n formattedString = parseBBCode(formattedString, {\n customInlineLevelBBCodeTags,\n customBlockLevelBBCodeTags,\n justEscapeSquareBrackets: true,\n });\n }\n\n formattedString = parseMarkdown(formattedString, parseBBCodeOption);\n\n // Remove trailing \\n\n formattedString = formattedString.replace(/\\n$/, '');\n\n if (parseBBCodeOption) {\n // Unescapes BB-Code brackets.\n formattedString = unescapeBBCodeSquareBrackets(formattedString);\n }\n } catch (error) {\n console.warn('[@chayns-components/format] Warning: Failed to parse markdown', error);\n }\n }\n\n // Parses BB-Code to HTML.\n if (parseBBCodeOption) {\n try {\n formattedString = parseBBCode(formattedString, {\n customInlineLevelBBCodeTags,\n customBlockLevelBBCodeTags,\n });\n formattedString = unescapeBBCodeSquareBrackets(formattedString);\n } catch (error) {\n console.warn('[@chayns-components/format] Warning: Failed to parse bb-code', error);\n }\n }\n\n return {\n html: formattedString,\n tables,\n };\n};\n"],"mappings":";;;;;;AACA,IAAAA,OAAA,GAAAC,OAAA;AACA,IAAAC,aAAA,GAAAD,OAAA;AAKA,IAAAE,eAAA,GAAAF,OAAA;AAYA;AACO,MAAMG,kBAAkB,GAAGA,CAC9BC,MAAc,EACdC,OAA6B,KACR;EACrB,IAAI,CAACD,MAAM,EAAE;IACT,OAAO;MACHE,IAAI,EAAE,EAAE;MACRC,MAAM,EAAE;IACZ,CAAC;EACL;EAEA,MAAM;IACFC,aAAa,EAAEC,mBAAmB,GAAG,IAAI;IACzCC,WAAW,EAAEC,iBAAiB,GAAG,KAAK;IACtCC,2BAA2B,GAAG,EAAE;IAChCC,0BAA0B,GAAG;EACjC,CAAC,GAAGR,OAAO,IAAI,CAAC,CAAC;EAEjB,IAAIS,eAAe,GAAGV,MAAM;;EAE5B;EACA,MAAMG,MAAqB,GAAG,EAAE;EAChC,IAAIE,mBAAmB,EAAE;IACrB,IAAI;MACAF,MAAM,CAACQ,IAAI,CAAC,GAAG,IAAAC,iCAAiB,EAACF,eAAe,CAAC,CAAC;IACtD,CAAC,CAAC,OAAOG,KAAK,EAAE;MACZC,OAAO,CAACC,IAAI,CACR,oEAAoE,EACpEF,KACJ,CAAC;IACL;EACJ;;EAEA;EACAH,eAAe,GAAG,IAAAM,wBAAgB,EAACN,eAAe,CAAC;;EAEnD;;EAEA;EACA,IAAIL,mBAAmB,EAAE;IACrB,IAAI;MACA,IAAIE,iBAAiB,EAAE;QACnB;QACAG,eAAe,GAAG,IAAAJ,yBAAW,EAACI,eAAe,EAAE;UAC3CF,2BAA2B;UAC3BC,0BAA0B;UAC1BQ,wBAAwB,EAAE;QAC9B,CAAC,CAAC;MACN;MAEAP,eAAe,GAAG,IAAAN,6BAAa,EAACM,eAAe,EAAEH,iBAAiB,CAAC;;MAEnE;MACAG,eA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