@chayns-components/format 5.0.0-beta.651 → 5.0.0-beta.682
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.
- package/lib/cjs/constants/format.js +8 -0
- package/lib/cjs/constants/format.js.map +1 -0
- package/lib/cjs/index.js +7 -0
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/types/format.js +2 -0
- package/lib/cjs/types/format.js.map +1 -0
- package/lib/cjs/utils/escape.js +1 -5
- package/lib/cjs/utils/escape.js.map +1 -1
- package/lib/cjs/utils/formatString/bb-code/findBBCode.js +6 -4
- package/lib/cjs/utils/formatString/bb-code/findBBCode.js.map +1 -1
- package/lib/cjs/utils/formatString/bb-code/formatBBCode.js +38 -38
- package/lib/cjs/utils/formatString/bb-code/formatBBCode.js.map +1 -1
- package/lib/cjs/utils/formatString/formatString.js +30 -42
- package/lib/cjs/utils/formatString/formatString.js.map +1 -1
- package/lib/cjs/utils/formatString/formatString.test.js +549 -0
- package/lib/cjs/utils/formatString/formatString.test.js.map +1 -0
- package/lib/cjs/utils/formatString/markdown/formatMarkdown.js +145 -30
- package/lib/cjs/utils/formatString/markdown/formatMarkdown.js.map +1 -1
- package/lib/esm/constants/format.js +2 -0
- package/lib/esm/constants/format.js.map +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/types/format.js +2 -0
- package/lib/esm/types/format.js.map +1 -0
- package/lib/esm/utils/escape.js +0 -3
- package/lib/esm/utils/escape.js.map +1 -1
- package/lib/esm/utils/formatString/bb-code/findBBCode.js +6 -4
- package/lib/esm/utils/formatString/bb-code/findBBCode.js.map +1 -1
- package/lib/esm/utils/formatString/bb-code/formatBBCode.js +38 -40
- package/lib/esm/utils/formatString/bb-code/formatBBCode.js.map +1 -1
- package/lib/esm/utils/formatString/formatString.js +33 -45
- package/lib/esm/utils/formatString/formatString.js.map +1 -1
- package/lib/esm/utils/formatString/formatString.test.js +547 -0
- package/lib/esm/utils/formatString/formatString.test.js.map +1 -0
- package/lib/esm/utils/formatString/markdown/formatMarkdown.js +141 -27
- package/lib/esm/utils/formatString/markdown/formatMarkdown.js.map +1 -1
- package/lib/types/constants/format.d.ts +1 -0
- package/lib/types/index.d.ts +2 -0
- package/lib/types/types/format.d.ts +5 -0
- package/lib/types/utils/escape.d.ts +0 -3
- package/lib/types/utils/formatString/bb-code/findBBCode.d.ts +2 -0
- package/lib/types/utils/formatString/bb-code/formatBBCode.d.ts +4 -2
- package/lib/types/utils/formatString/formatString.d.ts +1 -3
- package/lib/types/utils/formatString/formatString.test.d.ts +1 -0
- package/lib/types/utils/formatString/markdown/formatMarkdown.d.ts +3 -1
- package/package.json +6 -4
- package/lib/cjs/utils/formatString/markdown/InternalHTMLRenderer.js +0 -287
- package/lib/cjs/utils/formatString/markdown/InternalHTMLRenderer.js.map +0 -1
- package/lib/cjs/utils/formatString/markdown/formatMarkdownTable.js +0 -86
- package/lib/cjs/utils/formatString/markdown/formatMarkdownTable.js.map +0 -1
- package/lib/esm/utils/formatString/markdown/InternalHTMLRenderer.js +0 -278
- package/lib/esm/utils/formatString/markdown/InternalHTMLRenderer.js.map +0 -1
- package/lib/esm/utils/formatString/markdown/formatMarkdownTable.js +0 -78
- package/lib/esm/utils/formatString/markdown/formatMarkdownTable.js.map +0 -1
- package/lib/types/utils/formatString/markdown/InternalHTMLRenderer.d.ts +0 -5
- package/lib/types/utils/formatString/markdown/formatMarkdownTable.d.ts +0 -9
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatBBCode.js","names":["
|
|
1
|
+
{"version":3,"file":"formatBBCode.js","names":["BB_CODE_HTML_TAG_PREFIX","findFirstBBCode","BLOCK_LEVEL_TAGS","INLINE_LEVEL_TAGS","parseBBCode","text","options","customBlockLevelBBCodeTags","customBlockLevelTags","customInlineLevelBBCodeTags","customInlineLevelTags","justEscapeSquareBrackets","html","parseBehindIndex","length","htmlToParse","slice","firstBBCodeMatch","content","fullMatch","parameters","index","openingTag","closingTag","Tag","tag","toLowerCase","isValidTag","includes","isBlockLevelTag","parsedContent","indexOfFullMatch","indexOf","escapedOpeningTag","escapeBBCodeSquareBrackets","escapedClosingTag","replace","htmlAfterTag","isCustomTag","htmlTag","openingHtmlTag","Object","entries","map","_ref","key","value","join","closingHtmlTag","element","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('[', '‍[‍').replaceAll(']', '‍]‍');\n\nexport const unescapeBBCodeSquareBrackets = (text: string) =>\n text.replaceAll('‍[‍', '[').replaceAll('‍]‍', ']');\n"],"mappings":"AAAA,SAASA,uBAAuB,QAAQ,2BAA2B;AACnE,SAASC,eAAe,QAAQ,cAAc;AAE9C,MAAMC,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;AACA,OAAO,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;IAEh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,gBAAgB,GAAGkB,gBAAgB,GAAGkB,OAAO,CAACnC,MAAM;IACxD;EACJ;EAEA,OAAOF,IAAI;AACf,CAAC;AAED,OAAO,MAAMsB,0BAA0B,GAAI7B,IAAY,IACnDA,IAAI,CAAC6C,UAAU,CAAC,GAAG,EAAE,aAAa,CAAC,CAACA,UAAU,CAAC,GAAG,EAAE,aAAa,CAAC;AAEtE,OAAO,MAAMC,4BAA4B,GAAI9C,IAAY,IACrDA,IAAI,CAAC6C,UAAU,CAAC,aAAa,EAAE,GAAG,CAAC,CAACA,UAAU,CAAC,aAAa,EAAE,GAAG,CAAC","ignoreList":[]}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { escapeHtmlInText
|
|
2
|
-
import { parseBBCode } from './bb-code/formatBBCode';
|
|
3
|
-
import { parseMarkdown } from './markdown/formatMarkdown';
|
|
4
|
-
import { parseMarkdownTables } from './markdown/formatMarkdownTable';
|
|
1
|
+
import { escapeHtmlInText } from '../escape';
|
|
2
|
+
import { parseBBCode, unescapeBBCodeSquareBrackets } from './bb-code/formatBBCode';
|
|
3
|
+
import { getMarkdownTables, parseMarkdown } from './markdown/formatMarkdown';
|
|
5
4
|
// This function takes a string and returns formatted html as a string.
|
|
6
5
|
export const formatStringToHtml = (string, options) => {
|
|
7
6
|
if (!string) {
|
|
@@ -11,71 +10,60 @@ export const formatStringToHtml = (string, options) => {
|
|
|
11
10
|
};
|
|
12
11
|
}
|
|
13
12
|
const {
|
|
14
|
-
escapeHtml: escapeHtmlOption = false,
|
|
15
13
|
parseMarkdown: parseMarkdownOption = true,
|
|
16
|
-
parseMarkdownTables: parseMarkdownTablesOption = false,
|
|
17
14
|
parseBBCode: parseBBCodeOption = false,
|
|
18
15
|
customInlineLevelBBCodeTags = [],
|
|
19
16
|
customBlockLevelBBCodeTags = []
|
|
20
17
|
} = options || {};
|
|
21
18
|
let formattedString = string;
|
|
22
19
|
|
|
23
|
-
//
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// Escape BB-Code square brackets, to prevent conflicts between markdown and BB Code.
|
|
29
|
-
/* Conflict example:
|
|
30
|
-
When Sidekick detects a function call as an entity through NER, then the following text is returned.
|
|
31
|
-
'[nerReplace <params>]function[/nerReplace](<params>)'
|
|
32
|
-
Because '[/nerReplace](<params>)' is a valid Markdown link, the Markdown parser would interpret it as a link
|
|
33
|
-
and thus prevent the BB-Code parser from recognizing the BB-Code. Parsing the BB-Code first would prevent this
|
|
34
|
-
issue. Unfortunately the Markdown parser doesn't support this.
|
|
35
|
-
*/
|
|
36
|
-
const shouldTemporarilyEscapeBBCodeBrackets = parseMarkdownOption && parseBBCodeOption;
|
|
37
|
-
if (shouldTemporarilyEscapeBBCodeBrackets) {
|
|
20
|
+
// Needs to get the tables before escaping html and parsing bb-code, so the original content can be extracted.
|
|
21
|
+
const tables = [];
|
|
22
|
+
if (parseMarkdownOption) {
|
|
38
23
|
try {
|
|
39
|
-
|
|
40
|
-
justEscapeSquareBrackets: true
|
|
41
|
-
});
|
|
24
|
+
tables.push(...getMarkdownTables(formattedString));
|
|
42
25
|
} catch (error) {
|
|
43
|
-
console.warn('[@chayns-components/format] Warning: Failed to
|
|
26
|
+
console.warn('[@chayns-components/format] Warning: Failed to get markdown tables', error);
|
|
44
27
|
}
|
|
45
28
|
}
|
|
46
29
|
|
|
47
|
-
//
|
|
30
|
+
// Escape HTML entities.
|
|
31
|
+
formattedString = escapeHtmlInText(formattedString);
|
|
32
|
+
|
|
33
|
+
// 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.
|
|
34
|
+
|
|
35
|
+
// Parses markdown to HTML.
|
|
48
36
|
if (parseMarkdownOption) {
|
|
49
37
|
try {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
38
|
+
if (parseBBCodeOption) {
|
|
39
|
+
// Escapes BB-Code brackets.
|
|
40
|
+
formattedString = parseBBCode(formattedString, {
|
|
41
|
+
customInlineLevelBBCodeTags,
|
|
42
|
+
customBlockLevelBBCodeTags,
|
|
43
|
+
justEscapeSquareBrackets: true
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
formattedString = parseMarkdown(formattedString, parseBBCodeOption);
|
|
56
47
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
48
|
+
// Remove trailing \n
|
|
49
|
+
formattedString = formattedString.replace(/\n$/, '');
|
|
50
|
+
if (parseBBCodeOption) {
|
|
51
|
+
// Unescapes BB-Code brackets.
|
|
52
|
+
formattedString = unescapeBBCodeSquareBrackets(formattedString);
|
|
53
|
+
}
|
|
63
54
|
} catch (error) {
|
|
64
|
-
console.warn('[@chayns-components/format] Warning: Failed to parse markdown
|
|
55
|
+
console.warn('[@chayns-components/format] Warning: Failed to parse markdown', error);
|
|
65
56
|
}
|
|
66
57
|
}
|
|
67
58
|
|
|
68
|
-
//
|
|
69
|
-
if (shouldTemporarilyEscapeBBCodeBrackets) {
|
|
70
|
-
formattedString = unescapeSquareBrackets(formattedString);
|
|
71
|
-
}
|
|
59
|
+
// Parses BB-Code to HTML.
|
|
72
60
|
if (parseBBCodeOption) {
|
|
73
61
|
try {
|
|
74
62
|
formattedString = parseBBCode(formattedString, {
|
|
75
63
|
customInlineLevelBBCodeTags,
|
|
76
|
-
customBlockLevelBBCodeTags
|
|
77
|
-
justEscapeSquareBrackets: false
|
|
64
|
+
customBlockLevelBBCodeTags
|
|
78
65
|
});
|
|
66
|
+
formattedString = unescapeBBCodeSquareBrackets(formattedString);
|
|
79
67
|
} catch (error) {
|
|
80
68
|
console.warn('[@chayns-components/format] Warning: Failed to parse bb-code', error);
|
|
81
69
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatString.js","names":["escapeHtmlInText","
|
|
1
|
+
{"version":3,"file":"formatString.js","names":["escapeHtmlInText","parseBBCode","unescapeBBCodeSquareBrackets","getMarkdownTables","parseMarkdown","formatStringToHtml","string","options","html","tables","parseMarkdownOption","parseBBCodeOption","customInlineLevelBBCodeTags","customBlockLevelBBCodeTags","formattedString","push","error","console","warn","justEscapeSquareBrackets","replace"],"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,SAASA,gBAAgB,QAAQ,WAAW;AAC5C,SACIC,WAAW,EAEXC,4BAA4B,QACzB,wBAAwB;AAC/B,SAASC,iBAAiB,EAAEC,aAAa,QAAQ,2BAA2B;AAY5E;AACA,OAAO,MAAMC,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;IACFL,aAAa,EAAEM,mBAAmB,GAAG,IAAI;IACzCT,WAAW,EAAEU,iBAAiB,GAAG,KAAK;IACtCC,2BAA2B,GAAG,EAAE;IAChCC,0BAA0B,GAAG;EACjC,CAAC,GAAGN,OAAO,IAAI,CAAC,CAAC;EAEjB,IAAIO,eAAe,GAAGR,MAAM;;EAE5B;EACA,MAAMG,MAAqB,GAAG,EAAE;EAChC,IAAIC,mBAAmB,EAAE;IACrB,IAAI;MACAD,MAAM,CAACM,IAAI,CAAC,GAAGZ,iBAAiB,CAACW,eAAe,CAAC,CAAC;IACtD,CAAC,CAAC,OAAOE,KAAK,EAAE;MACZC,OAAO,CAACC,IAAI,CACR,oEAAoE,EACpEF,KACJ,CAAC;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