react-pdf-html-flabs-2 1.0.6 → 1.0.9
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/dist/htmlOverrides.d.ts +1 -0
- package/dist/htmlOverrides.js +17 -0
- package/dist/htmlOverrides.js.map +1 -0
- package/dist/parse.js +3 -1
- package/dist/parse.js.map +1 -1
- package/dist/renderers.js +9 -1
- package/dist/renderers.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function replaceText(input: string): string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.replaceText = void 0;
|
|
4
|
+
function replaceText(input) {
|
|
5
|
+
const replaceProps = [
|
|
6
|
+
{
|
|
7
|
+
_p: '·',
|
|
8
|
+
_v: '<span style="font-weight:600;">.</span>',
|
|
9
|
+
},
|
|
10
|
+
];
|
|
11
|
+
replaceProps.forEach((prop) => {
|
|
12
|
+
input = input.replace(new RegExp(prop._p, 'g'), prop._v);
|
|
13
|
+
});
|
|
14
|
+
return input;
|
|
15
|
+
}
|
|
16
|
+
exports.replaceText = replaceText;
|
|
17
|
+
//# sourceMappingURL=htmlOverrides.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"htmlOverrides.js","sourceRoot":"","sources":["../src/htmlOverrides.ts"],"names":[],"mappings":";;;AAAA,SAAgB,WAAW,CAAC,KAAa;IACvC,MAAM,YAAY,GAAG;QACnB;YACE,EAAE,EAAE,UAAU;YACd,EAAE,EAAE,yCAAyC;SAC9C;KACF,CAAC;IACF,YAAY,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QAC5B,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,EAAE,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;IAC3D,CAAC,CAAC,CAAC;IACH,OAAO,KAAK,CAAC;AACf,CAAC;AAXD,kCAWC","sourcesContent":["export function replaceText(input: string): string {\n const replaceProps = [\n {\n _p: '·',\n _v: '<span style=\"font-weight:600;\">.</span>',\n },\n ];\n replaceProps.forEach((prop) => {\n input = input.replace(new RegExp(prop._p, 'g'), prop._v);\n });\n return input;\n}\n"]}
|
package/dist/parse.js
CHANGED
|
@@ -7,6 +7,7 @@ exports.convertNode = exports.convertElementStyle = exports.convertStylesheet =
|
|
|
7
7
|
const node_html_parser_1 = require("node-html-parser");
|
|
8
8
|
const css_tree_1 = __importDefault(require("css-tree"));
|
|
9
9
|
const supportedStyles_1 = __importDefault(require("./supportedStyles"));
|
|
10
|
+
const htmlOverrides_1 = require("./htmlOverrides");
|
|
10
11
|
const camelize = require('camelize');
|
|
11
12
|
const convertRule = (rule, source = 'style') => {
|
|
12
13
|
const declarations = rule.children
|
|
@@ -160,7 +161,8 @@ const convertNode = (node) => {
|
|
|
160
161
|
};
|
|
161
162
|
exports.convertNode = convertNode;
|
|
162
163
|
const parseHtml = (text) => {
|
|
163
|
-
const
|
|
164
|
+
const overideText = (0, htmlOverrides_1.replaceText)(text);
|
|
165
|
+
const html = (0, node_html_parser_1.parse)(overideText, { comment: false });
|
|
164
166
|
const stylesheets = html
|
|
165
167
|
.querySelectorAll('style')
|
|
166
168
|
.map((styleNode) => styleNode.childNodes.map((textNode) => textNode.rawText.trim()).join('\n'))
|
package/dist/parse.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":";;;;;;AAAA,uDAM0B;AAE1B,wDAA+E;AAC/E,wEAAgD;AAEhD,MAAM,QAAQ,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;AAc9B,MAAM,WAAW,GAAG,CACzB,IAAW,EACX,SAAiB,OAAO,EACb,EAAE;IACb,MAAM,YAAY,GAAG,IAAI,CAAC,QA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AAM,IAAI,GAAG,IAAA,wBAAK,EAAC,IAAI,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;IAC7C,MAAM,WAAW,GAAG,IAAI;SACrB,gBAAgB,CAAC,OAAO,CAAC;SACzB,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CACjB,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAC3E;SACA,MAAM,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;SAClC,GAAG,CAAC,yBAAiB,CAAC,CAAC;IAC1B,OAAO;QACL,WAAW;QACX,WAAW,EAAE,IAAA,mBAAW,EAAC,IAAI,CAAgB;KAC9C,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe,SAAS,CAAC","sourcesContent":["import {\n HTMLElement,\n Node as HTMLNode,\n NodeType,\n parse,\n TextNode,\n} from 'node-html-parser';\nimport { Tag } from './tags';\nimport cssTree, { Block, Declaration, List, Rule, StyleSheet } from 'css-tree';\nimport supportedStyles from './supportedStyles';\nimport { HtmlStyle, HtmlStyles } from './styles';\nconst camelize = require('camelize');\n\nexport type HtmlContent = (HtmlElement | string)[];\n\nexport type HtmlElement = HTMLElement & {\n tag: Tag | 'string';\n parentNode: HtmlElement;\n style: HtmlStyle[];\n content: HtmlContent;\n indexOfType: number;\n querySelectorAll: (selector: string) => HtmlElement[];\n querySelector: (selector: string) => HtmlElement;\n};\n\nexport const convertRule = (\n rule: Block,\n source: string = 'style'\n): HtmlStyle => {\n const declarations = rule.children\n .filter((declaration) => declaration.type === 'Declaration')\n .toArray() as Declaration[];\n\n return declarations\n .map((entry) => ({\n ...entry,\n property: camelize(entry.property as string),\n }))\n .reduce((style, { property, value }: Declaration) => {\n let valueString = cssTree.generate(value);\n if (property && value !== undefined) {\n if (property == 'fontFamily') {\n valueString = valueString.replace(/[\"']+/g, '');\n if (valueString.includes(',')) {\n const reduced = valueString.split(',', 2)[0];\n // console.warn(\n // `react-pdf doesn't support fontFamily lists like \"${valueString}\". Reducing to \"${reduced}\".`\n // );\n }\n return style;\n } else if (!supportedStyles.includes(property)) {\n if (\n (property === 'background' &&\n /^#?[a-zA-Z0-9]+$/.test(valueString)) ||\n /^rgba?\\([0-9, ]+\\)$/i.test(valueString) ||\n /^hsla?\\([0-9.%, ]+\\)$/i.test(valueString)\n ) {\n property = 'backgroundColor';\n } else {\n // console.warn(`${source}: Found unsupported style \"${property}\"`, {\n // property,\n // value,\n // });\n return style;\n }\n }\n if (property.startsWith('border')) {\n if (typeof valueString === 'string' && valueString.trim() !== '') {\n const borderParts = valueString.split(' ').filter(Boolean);\n const widthRegex = /^\\d+(\\.\\d+)?(px|pt|em|rem|%)?$/i;\n const styleRegex =\n /^(solid|dashed|dotted|double|groove|ridge|inset|outset|none|hidden)$/i;\n const colorRegex = /^#|rgb|rgba|hsl|hsla|[a-zA-Z]+$/;\n\n let width = '1px';\n let style = 'solid';\n let color = 'black';\n\n let explicitNone = false;\n\n if (borderParts.length === 3) {\n borderParts.forEach((part) => {\n const partLower = part.toLowerCase();\n if (widthRegex.test(partLower)) {\n width = part;\n } else if (styleRegex.test(partLower)) {\n if (partLower === 'none' || partLower === 'hidden') {\n explicitNone = true;\n } else {\n style = partLower;\n }\n } else if (colorRegex.test(part)) {\n color = part;\n }\n });\n valueString = `${width} ${style} ${color}`;\n }\n {\n console.warn(\n `${source}: Unexpected border format \"${valueString}\"`\n );\n }\n\n if (explicitNone) {\n return style; // ✅ do not apply this style at all\n }\n } else {\n console.warn(`${source}: Invalid border value \"${valueString}\"`);\n return style;\n }\n }\n\n if (property == 'border' && valueString == 'none') valueString = '0px';\n style[property as keyof HtmlStyle] = valueString;\n }\n return style;\n }, {} as HtmlStyle);\n};\n\nexport const convertStylesheet = (stylesheet: string): HtmlStyles => {\n const response = {} as HtmlStyles;\n try {\n const parsed = cssTree.parse(stylesheet) as StyleSheet;\n const rules = parsed.children.filter(\n (rule) => rule.type === 'Rule' && rule.prelude?.type === 'SelectorList'\n ) as List<Rule>;\n rules.forEach((rule) => {\n const style = convertRule(rule.block);\n if (rule.prelude.type !== 'SelectorList') {\n return;\n }\n rule.prelude.children.forEach((selector) => {\n const selectorString = cssTree.generate(selector);\n response[selectorString] = style;\n });\n });\n } catch (e) {\n console.error(`Error parsing stylesheet: \"${stylesheet}\"`, e);\n }\n return response;\n};\n\nexport const convertElementStyle = (\n styleAttr: string,\n tag: string\n): HtmlStyle | undefined => {\n try {\n const parsed = cssTree.parse(`${tag} { ${styleAttr} }`) as StyleSheet;\n const rules = parsed.children.filter(\n (rule) => rule.type === 'Rule' && rule.prelude?.type === 'SelectorList'\n ) as List<Rule>;\n const firstRule = rules.first();\n return firstRule ? convertRule(firstRule.block, tag) : undefined;\n } catch (e) {\n console.error(\n `Error parsing style attribute \"${styleAttr}\" for tag: ${tag}`,\n e\n );\n }\n};\n\nexport const convertNode = (node: HTMLNode): HtmlElement | string => {\n if (node.nodeType === NodeType.TEXT_NODE) {\n return (node as TextNode).rawText;\n }\n if (node.nodeType === NodeType.COMMENT_NODE) {\n return '';\n }\n if (node.nodeType !== NodeType.ELEMENT_NODE) {\n throw new Error('Not sure what this is');\n }\n const html = node as HTMLElement;\n const content = html.childNodes.map(convertNode);\n const kindCounters: Record<string, number> = {};\n content.forEach((child) => {\n if (typeof child !== 'string') {\n child.indexOfType =\n child.tag in kindCounters\n ? (kindCounters[child.tag] = kindCounters[child.tag] + 1)\n : (kindCounters[child.tag] = 0);\n }\n });\n\n let style: HtmlStyle | undefined;\n if (html.attributes.style && html.attributes.style.trim()) {\n style = convertElementStyle(html.attributes.style, html.tagName);\n }\n\n return Object.assign(html, {\n tag: (html.tagName || '').toLowerCase() as Tag | string,\n style: style ? [style] : [],\n content,\n indexOfType: 0,\n }) as HtmlElement;\n};\n\nconst parseHtml = (\n text: string\n): { stylesheets: HtmlStyles[]; rootElement: HtmlElement } => {\n const html = parse(text, { comment: false });\n const stylesheets = html\n .querySelectorAll('style')\n .map((styleNode) =>\n styleNode.childNodes.map((textNode) => textNode.rawText.trim()).join('\\n')\n )\n .filter((styleText) => !!styleText)\n .map(convertStylesheet);\n return {\n stylesheets,\n rootElement: convertNode(html) as HtmlElement,\n };\n};\n\nexport default parseHtml;\n"]}
|
|
1
|
+
{"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":";;;;;;AAAA,uDAM0B;AAE1B,wDAA+E;AAC/E,wEAAgD;AAEhD,mDAA8C;AAC9C,MAAM,QAAQ,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;AAc9B,MAAM,WAAW,GAAG,CACzB,IAAW,EACX,SAAiB,OAAO,EACb,EAAE;IACb,MAAM,YAAY,GAAG,IAAI,CAAC,QAAQ;SAC/B,MAAM,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,WAAW,CAAC,IAAI,KAAK,aAAa,CAAC;SAC3D,OAAO,EAAmB,CAAC;IAE9B,OAAO,YAAY;SAChB,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,iCACX,KAAK,KACR,QAAQ,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAkB,CAAC,IAC5C,CAAC;SACF,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAe,EAAE,EAAE;QAClD,IAAI,WAAW,GAAG,kBAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC1C,IAAI,QAAQ,IAAI,KAAK,KAAK,SAAS,EAAE;YACnC,IAAI,QAAQ,IAAI,YAAY,EAAE;gBAC5B,WAAW,GAAG,WAAW,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;gBAChD,IAAI,WAAW,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;oBAC7B,MAAM,OAAO,GAAG,WAAW,CAAC,KAAK,CA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TMLNode,\n NodeType,\n parse,\n TextNode,\n} from 'node-html-parser';\nimport { Tag } from './tags';\nimport cssTree, { Block, Declaration, List, Rule, StyleSheet } from 'css-tree';\nimport supportedStyles from './supportedStyles';\nimport { HtmlStyle, HtmlStyles } from './styles';\nimport { replaceText } from './htmlOverrides';\nconst camelize = require('camelize');\n\nexport type HtmlContent = (HtmlElement | string)[];\n\nexport type HtmlElement = HTMLElement & {\n tag: Tag | 'string';\n parentNode: HtmlElement;\n style: HtmlStyle[];\n content: HtmlContent;\n indexOfType: number;\n querySelectorAll: (selector: string) => HtmlElement[];\n querySelector: (selector: string) => HtmlElement;\n};\n\nexport const convertRule = (\n rule: Block,\n source: string = 'style'\n): HtmlStyle => {\n const declarations = rule.children\n .filter((declaration) => declaration.type === 'Declaration')\n .toArray() as Declaration[];\n\n return declarations\n .map((entry) => ({\n ...entry,\n property: camelize(entry.property as string),\n }))\n .reduce((style, { property, value }: Declaration) => {\n let valueString = cssTree.generate(value);\n if (property && value !== undefined) {\n if (property == 'fontFamily') {\n valueString = valueString.replace(/[\"']+/g, '');\n if (valueString.includes(',')) {\n const reduced = valueString.split(',', 2)[0];\n // console.warn(\n // `react-pdf doesn't support fontFamily lists like \"${valueString}\". Reducing to \"${reduced}\".`\n // );\n }\n return style;\n } else if (!supportedStyles.includes(property)) {\n if (\n (property === 'background' &&\n /^#?[a-zA-Z0-9]+$/.test(valueString)) ||\n /^rgba?\\([0-9, ]+\\)$/i.test(valueString) ||\n /^hsla?\\([0-9.%, ]+\\)$/i.test(valueString)\n ) {\n property = 'backgroundColor';\n } else {\n // console.warn(`${source}: Found unsupported style \"${property}\"`, {\n // property,\n // value,\n // });\n return style;\n }\n }\n if (property.startsWith('border')) {\n if (typeof valueString === 'string' && valueString.trim() !== '') {\n const borderParts = valueString.split(' ').filter(Boolean);\n const widthRegex = /^\\d+(\\.\\d+)?(px|pt|em|rem|%)?$/i;\n const styleRegex =\n /^(solid|dashed|dotted|double|groove|ridge|inset|outset|none|hidden)$/i;\n const colorRegex = /^#|rgb|rgba|hsl|hsla|[a-zA-Z]+$/;\n\n let width = '1px';\n let style = 'solid';\n let color = 'black';\n\n let explicitNone = false;\n\n if (borderParts.length === 3) {\n borderParts.forEach((part) => {\n const partLower = part.toLowerCase();\n if (widthRegex.test(partLower)) {\n width = part;\n } else if (styleRegex.test(partLower)) {\n if (partLower === 'none' || partLower === 'hidden') {\n explicitNone = true;\n } else {\n style = partLower;\n }\n } else if (colorRegex.test(part)) {\n color = part;\n }\n });\n valueString = `${width} ${style} ${color}`;\n }\n {\n console.warn(\n `${source}: Unexpected border format \"${valueString}\"`\n );\n }\n\n if (explicitNone) {\n return style; // ✅ do not apply this style at all\n }\n } else {\n console.warn(`${source}: Invalid border value \"${valueString}\"`);\n return style;\n }\n }\n\n if (property == 'border' && valueString == 'none') valueString = '0px';\n style[property as keyof HtmlStyle] = valueString;\n }\n return style;\n }, {} as HtmlStyle);\n};\n\nexport const convertStylesheet = (stylesheet: string): HtmlStyles => {\n const response = {} as HtmlStyles;\n try {\n const parsed = cssTree.parse(stylesheet) as StyleSheet;\n const rules = parsed.children.filter(\n (rule) => rule.type === 'Rule' && rule.prelude?.type === 'SelectorList'\n ) as List<Rule>;\n rules.forEach((rule) => {\n const style = convertRule(rule.block);\n if (rule.prelude.type !== 'SelectorList') {\n return;\n }\n rule.prelude.children.forEach((selector) => {\n const selectorString = cssTree.generate(selector);\n response[selectorString] = style;\n });\n });\n } catch (e) {\n console.error(`Error parsing stylesheet: \"${stylesheet}\"`, e);\n }\n return response;\n};\n\nexport const convertElementStyle = (\n styleAttr: string,\n tag: string\n): HtmlStyle | undefined => {\n try {\n const parsed = cssTree.parse(`${tag} { ${styleAttr} }`) as StyleSheet;\n const rules = parsed.children.filter(\n (rule) => rule.type === 'Rule' && rule.prelude?.type === 'SelectorList'\n ) as List<Rule>;\n const firstRule = rules.first();\n return firstRule ? convertRule(firstRule.block, tag) : undefined;\n } catch (e) {\n console.error(\n `Error parsing style attribute \"${styleAttr}\" for tag: ${tag}`,\n e\n );\n }\n};\n\nexport const convertNode = (node: HTMLNode): HtmlElement | string => {\n if (node.nodeType === NodeType.TEXT_NODE) {\n return (node as TextNode).rawText;\n }\n if (node.nodeType === NodeType.COMMENT_NODE) {\n return '';\n }\n if (node.nodeType !== NodeType.ELEMENT_NODE) {\n throw new Error('Not sure what this is');\n }\n const html = node as HTMLElement;\n const content = html.childNodes.map(convertNode);\n const kindCounters: Record<string, number> = {};\n content.forEach((child) => {\n if (typeof child !== 'string') {\n child.indexOfType =\n child.tag in kindCounters\n ? (kindCounters[child.tag] = kindCounters[child.tag] + 1)\n : (kindCounters[child.tag] = 0);\n }\n });\n\n let style: HtmlStyle | undefined;\n if (html.attributes.style && html.attributes.style.trim()) {\n style = convertElementStyle(html.attributes.style, html.tagName);\n }\n\n return Object.assign(html, {\n tag: (html.tagName || '').toLowerCase() as Tag | string,\n style: style ? [style] : [],\n content,\n indexOfType: 0,\n }) as HtmlElement;\n};\n\nconst parseHtml = (\n text: string\n): { stylesheets: HtmlStyles[]; rootElement: HtmlElement } => {\n const overideText = replaceText(text);\n const html = parse(overideText, { comment: false });\n const stylesheets = html\n .querySelectorAll('style')\n .map((styleNode) =>\n styleNode.childNodes.map((textNode) => textNode.rawText.trim()).join('\\n')\n )\n .filter((styleText) => !!styleText)\n .map(convertStylesheet);\n return {\n stylesheets,\n rootElement: convertNode(html) as HtmlElement,\n };\n};\n\nexport default parseHtml;\n"]}
|
package/dist/renderers.js
CHANGED
|
@@ -131,7 +131,15 @@ const renderers = {
|
|
|
131
131
|
dimensions.height = height;
|
|
132
132
|
}
|
|
133
133
|
const finalStyles = Object.assign({}, ...style, dimensions);
|
|
134
|
-
return react_1.default.createElement(renderer_1.Image, { style: finalStyles,
|
|
134
|
+
return (react_1.default.createElement(renderer_1.Image, { style: finalStyles, source: {
|
|
135
|
+
uri: element.attributes.src,
|
|
136
|
+
body: null,
|
|
137
|
+
method: 'GET',
|
|
138
|
+
headers: {
|
|
139
|
+
'Cache-Control': 'no-cache',
|
|
140
|
+
'Access-Control-Allow-Origin': '*',
|
|
141
|
+
},
|
|
142
|
+
} }));
|
|
135
143
|
},
|
|
136
144
|
table: ({ element, style, children }) => {
|
|
137
145
|
var _a;
|
package/dist/renderers.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"renderers.js","sourceRoot":"","sources":["../src/renderers.tsx"],"names":[],"mappings":";;;;;;AAAA,kDAA0B;AAC1B,kDAA8D;AAI9D,iDAAsE;AAE/D,MAAM,UAAU,GAAiB,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,6DAAK,CAAC;AAAnD,QAAA,UAAU,cAAyC;AAEzD,MAAM,iBAAiB,GAA2C,CAAC,EACxE,QAAQ,GACT,EAAE,EAAE,CAAC,QAAQ,CAAC;AAFF,QAAA,iBAAiB,qBAEf;AAER,MAAM,WAAW,GAAiB,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,CAChE,8BAAC,eAAI,IAAC,KAAK,EAAE,KAAK,IAAG,QAAQ,CAAQ,CACtC,CAAC;AAFW,QAAA,WAAW,eAEtB;AAEK,MAAM,YAAY,GAAiB,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,CACjE,8BAAC,eAAI,IAAC,KAAK,EAAE,KAAK,IAAG,QAAQ,CAAQ,CACtC,CAAC;AAFW,QAAA,YAAY,gBAEvB;AAEK,MAAM,UAAU,GAAiB,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE;IACvE,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAA4B,CAAC;IAClE,MAAM,eAAe,GAAG,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,UAAiB,CAAC;IACjD,IAAI,CAAC,KAAK,EAAE;QACV,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;KAC3D;IAC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CAAC,EAAE,EAAE,GAAG,KAAK,CAAC,CAAC;QAChD,OAAO,WAAW,CAAC,MAAM,CAAC;QAC1B,OAAO,CACL,8BAAC,eAAI,IAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,WAAW,IACjC,QAAQ,CACJ,CACR,CAAC;IACJ,CAAC;IACD,EAAE,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,CACjB,8BAAC,eAAI,IAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,IAC3B,IAAI,CACA,CACR;IACD,EAAE,EAAE,kBAAU;IACd,EAAE,EAAE,kBAAU;CACf,CAAC;AAEF,kBAAe,SAAS,CAAC","sourcesContent":["import React from 'react';\nimport { Link, Text, View, Image } from '@react-pdf/renderer';\nimport { HtmlRenderer, HtmlRenderers } from './render';\nimport { HtmlElement } from './parse';\nimport { HtmlStyle } from './styles';\nimport { lowerAlpha, orderedAlpha, upperAlpha } from './ordered.type';\n\nexport const renderNoop: HtmlRenderer = ({ children }) => <></>;\n\nexport const renderPassThrough: React.FC<React.PropsWithChildren<any>> = ({\n children,\n}) => children;\n\nexport const renderBlock: HtmlRenderer = ({ style, children }) => (\n <View style={style}>{children}</View>\n);\n\nexport const renderInline: HtmlRenderer = ({ style, children }) => (\n <Text style={style}>{children}</Text>\n);\n\nexport const renderCell: HtmlRenderer = ({ style, element, children }) => {\n const table = element.closest('table') as HtmlElement | undefined;\n const tableAttributes = table?.attributes as any;\n if (!table) {\n throw new Error('td element rendered outside of a table');\n }\n const combinedStyle = style.reduce(\n (acc, current) => Object.assign(acc, current),\n {} as HtmlStyle\n );\n const tableStyles = table.style.reduce(\n (combined, tableStyle) => Object.assign(combined, tableStyle),\n {} as HtmlStyle\n );\n\n const baseStyles: HtmlStyle = {\n border: tableStyles.border,\n borderColor: tableStyles.borderColor,\n borderWidth: tableStyles.borderWidth,\n borderStyle: tableStyles.borderStyle,\n };\n if (\n (tableStyles as any).borderSpacing &&\n (tableStyles as any).borderCollapse !== 'collapse'\n ) {\n baseStyles.borderWidth = tableStyles.borderWidth;\n baseStyles.margin = (tableStyles as any).borderSpacing;\n } else {\n baseStyles.borderRightWidth = 0;\n baseStyles.borderBottomWidth = 0;\n if (element.indexOfType !== 0) {\n baseStyles.borderLeftWidth = tableStyles.borderWidth;\n baseStyles.borderTopWidth = tableStyles.borderWidth;\n }\n }\n\n if (tableAttributes?.border == 0) {\n baseStyles.borderRightWidth = '0px';\n baseStyles.borderBottomWidth = '0px';\n baseStyles.borderTopWidth = '0px';\n baseStyles.borderLeftWidth = '0px';\n }\n const overrides: HtmlStyle = {};\n // if (element.attributes && element.attributes.colspan) {\n // const colspan = parseInt(element.attributes.colspan, 10);\n // if (!isNaN(colspan)) {\n // overrides.flexBasis = colspan;\n // }\n // }\n if (combinedStyle.textAlign == 'center') {\n overrides.alignItems = 'center';\n }\n\n if (combinedStyle.verticalAlign == 'center') {\n overrides.justifyContent = 'center';\n }\n\n const finalStyles = Object.assign({}, baseStyles, combinedStyle, overrides);\n if (!finalStyles.width) finalStyles.flex = 1;\n delete finalStyles.height;\n\n return <View style={finalStyles}>{children}</View>;\n};\n\nconst renderers: HtmlRenderers = {\n style: renderNoop,\n script: renderNoop,\n html: renderPassThrough,\n li: ({ element, stylesheets, style, children }) => {\n const bulletStyles = stylesheets.map((stylesheet) => stylesheet.li_bullet);\n const contentStyles = stylesheets.map(\n (stylesheet) => stylesheet.li_content\n );\n const list: HtmlElement = element.closest('ol, ul') as HtmlElement;\n const ordered = list?.tag === 'ol' || element.parentNode.tag === 'ol';\n const listStyle =\n list?.style?.reduce(\n (combined, listStyle) => Object.assign(combined, listStyle),\n {} as HtmlStyle\n ) || {};\n const itemStyle = element.style.reduce(\n (combined, itemStyle) => Object.assign(combined, itemStyle),\n {} as HtmlStyle\n );\n const listStyleType =\n itemStyle.listStyleType ||\n itemStyle.listStyle ||\n listStyle.listStyleType ||\n listStyle.listStyle ||\n '';\n\n let bullet;\n if (listStyleType.includes('none')) {\n bullet = false;\n } else if (listStyleType.includes('url(')) {\n bullet = (\n <Image\n src={listStyleType.match(/\\((.*?)\\)/)[1].replace(/(['\"])/g, '')}\n />\n );\n } else if (ordered) {\n if (lowerAlpha.includes(listStyleType)) {\n bullet = (\n <Text>{orderedAlpha[element.indexOfType].toLowerCase()}.</Text>\n );\n } else if (upperAlpha.includes(listStyleType)) {\n bullet = (\n <Text>{orderedAlpha[element.indexOfType].toUpperCase()}.</Text>\n );\n } else {\n bullet = <Text>{element.indexOfType + 1}.</Text>;\n }\n } else {\n // if (listStyleType.includes('square')) {\n // bullet = <Text>■</Text>;\n // } else {\n bullet = <Text>•</Text>;\n // }\n }\n return (\n <View style={style}>\n {bullet && <View style={bulletStyles}>{bullet}</View>}\n <View style={contentStyles}>{children}</View>\n </View>\n );\n },\n a: ({ style, element, children }) => (\n <Link style={style} src={element.attributes.href}>\n {children}\n </Link>\n ),\n img: ({ style, element }) => {\n const { width, height } = element.attributes;\n const dimensions: any = {};\n if (width) {\n dimensions.width = width;\n }\n if (height) {\n dimensions.height = height;\n }\n const finalStyles = Object.assign({}, ...style, dimensions);\n return <Image style={finalStyles} src={element.attributes.src} />;\n },\n table: ({ element, style, children }) => {\n const tableStyles = element.style.reduce(\n (combined, tableStyle) => Object.assign(combined, tableStyle),\n {} as HtmlStyle\n );\n const { border } = element.attributes as any;\n const overrides: HtmlStyle = {};\n if (\n !(tableStyles as any).borderSpacing ||\n (tableStyles as any).borderCollapse === 'collapse'\n ) {\n overrides.borderLeftWidth = 0;\n overrides.borderTopWidth = 0;\n }\n\n if (border == 0) {\n overrides.borderWidth = '0px';\n overrides.borderStyle = 'none';\n }\n const borderColor = style.find(\n (s) => s.borderColor && s.borderColor !== 'gray'\n )?.borderColor;\n if (borderColor) {\n overrides.borderColor = borderColor;\n }\n const finalStyles = Object.assign({}, ...style, overrides);\n delete finalStyles.height;\n return <View style={finalStyles}>{children}</View>;\n },\n tr: ({ style, children }) => {\n const finalStyles = Object.assign({}, ...style);\n delete finalStyles.height;\n return (\n <View wrap={true} style={finalStyles}>\n {children}\n </View>\n );\n },\n br: ({ style }) => (\n <Text wrap={true} style={style}>\n {'\\n'}\n </Text>\n ),\n td: renderCell,\n th: renderCell,\n};\n\nexport default renderers;\n"]}
|
|
1
|
+
{"version":3,"file":"renderers.js","sourceRoot":"","sources":["../src/renderers.tsx"],"names":[],"mappings":";;;;;;AAAA,kDAA0B;AAC1B,kDAA8D;AAI9D,iDAAsE;AAE/D,MAAM,UAAU,GAAiB,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,6DAAK,CAAC;AAAnD,QAAA,UAAU,cAAyC;AAEzD,MAAM,iBAAiB,GAA2C,CAAC,EACxE,QAAQ,GACT,EAAE,EAAE,CAAC,QAAQ,CAAC;AAFF,QAAA,iBAAiB,qBAEf;AAER,MAAM,WAAW,GAAiB,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,CAChE,8BAAC,eAAI,IAAC,KAAK,EAAE,KAAK,IAAG,QAAQ,CAAQ,CACtC,CAAC;AAFW,QAAA,WAAW,eAEtB;AAEK,MAAM,YAAY,GAAiB,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,CACjE,8BAAC,eAAI,IAAC,KAAK,EAAE,KAAK,IAAG,QAAQ,CAAQ,CACtC,CAAC;AAFW,QAAA,YAAY,gBAEvB;AAEK,MAAM,UAAU,GAAiB,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE;IACvE,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAA4B,CAAC;IAClE,MAAM,eAAe,GAAG,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,UAAiB,CAAC;IACjD,IAAI,CAAC,KAAK,EAAE;QACV,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;KAC3D;IACD,MAAM,aAAa,GAAG,KAAK,CAAC,M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AAC;IACrD,CAAC;IACD,EAAE,EAAE,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE;QAC1B,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,GAAG,KAAK,CAAC,CAAC;QAChD,OAAO,WAAW,CAAC,MAAM,CAAC;QAC1B,OAAO,CACL,8BAAC,eAAI,IAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,WAAW,IACjC,QAAQ,CACJ,CACR,CAAC;IACJ,CAAC;IACD,EAAE,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,CACjB,8BAAC,eAAI,IAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,IAC3B,IAAI,CACA,CACR;IACD,EAAE,EAAE,kBAAU;IACd,EAAE,EAAE,kBAAU;CACf,CAAC;AAEF,kBAAe,SAAS,CAAC","sourcesContent":["import React from 'react';\nimport { Link, Text, View, Image } from '@react-pdf/renderer';\nimport { HtmlRenderer, HtmlRenderers } from './render';\nimport { HtmlElement } from './parse';\nimport { HtmlStyle } from './styles';\nimport { lowerAlpha, orderedAlpha, upperAlpha } from './ordered.type';\n\nexport const renderNoop: HtmlRenderer = ({ children }) => <></>;\n\nexport const renderPassThrough: React.FC<React.PropsWithChildren<any>> = ({\n children,\n}) => children;\n\nexport const renderBlock: HtmlRenderer = ({ style, children }) => (\n <View style={style}>{children}</View>\n);\n\nexport const renderInline: HtmlRenderer = ({ style, children }) => (\n <Text style={style}>{children}</Text>\n);\n\nexport const renderCell: HtmlRenderer = ({ style, element, children }) => {\n const table = element.closest('table') as HtmlElement | undefined;\n const tableAttributes = table?.attributes as any;\n if (!table) {\n throw new Error('td element rendered outside of a table');\n }\n const combinedStyle = style.reduce(\n (acc, current) => Object.assign(acc, current),\n {} as HtmlStyle\n );\n const tableStyles = table.style.reduce(\n (combined, tableStyle) => Object.assign(combined, tableStyle),\n {} as HtmlStyle\n );\n\n const baseStyles: HtmlStyle = {\n border: tableStyles.border,\n borderColor: tableStyles.borderColor,\n borderWidth: tableStyles.borderWidth,\n borderStyle: tableStyles.borderStyle,\n };\n if (\n (tableStyles as any).borderSpacing &&\n (tableStyles as any).borderCollapse !== 'collapse'\n ) {\n baseStyles.borderWidth = tableStyles.borderWidth;\n baseStyles.margin = (tableStyles as any).borderSpacing;\n } else {\n baseStyles.borderRightWidth = 0;\n baseStyles.borderBottomWidth = 0;\n if (element.indexOfType !== 0) {\n baseStyles.borderLeftWidth = tableStyles.borderWidth;\n baseStyles.borderTopWidth = tableStyles.borderWidth;\n }\n }\n\n if (tableAttributes?.border == 0) {\n baseStyles.borderRightWidth = '0px';\n baseStyles.borderBottomWidth = '0px';\n baseStyles.borderTopWidth = '0px';\n baseStyles.borderLeftWidth = '0px';\n }\n const overrides: HtmlStyle = {};\n // if (element.attributes && element.attributes.colspan) {\n // const colspan = parseInt(element.attributes.colspan, 10);\n // if (!isNaN(colspan)) {\n // overrides.flexBasis = colspan;\n // }\n // }\n if (combinedStyle.textAlign == 'center') {\n overrides.alignItems = 'center';\n }\n\n if (combinedStyle.verticalAlign == 'center') {\n overrides.justifyContent = 'center';\n }\n\n const finalStyles = Object.assign({}, baseStyles, combinedStyle, overrides);\n if (!finalStyles.width) finalStyles.flex = 1;\n delete finalStyles.height;\n\n return <View style={finalStyles}>{children}</View>;\n};\n\nconst renderers: HtmlRenderers = {\n style: renderNoop,\n script: renderNoop,\n html: renderPassThrough,\n li: ({ element, stylesheets, style, children }) => {\n const bulletStyles = stylesheets.map((stylesheet) => stylesheet.li_bullet);\n const contentStyles = stylesheets.map(\n (stylesheet) => stylesheet.li_content\n );\n const list: HtmlElement = element.closest('ol, ul') as HtmlElement;\n const ordered = list?.tag === 'ol' || element.parentNode.tag === 'ol';\n const listStyle =\n list?.style?.reduce(\n (combined, listStyle) => Object.assign(combined, listStyle),\n {} as HtmlStyle\n ) || {};\n const itemStyle = element.style.reduce(\n (combined, itemStyle) => Object.assign(combined, itemStyle),\n {} as HtmlStyle\n );\n const listStyleType =\n itemStyle.listStyleType ||\n itemStyle.listStyle ||\n listStyle.listStyleType ||\n listStyle.listStyle ||\n '';\n\n let bullet;\n if (listStyleType.includes('none')) {\n bullet = false;\n } else if (listStyleType.includes('url(')) {\n bullet = (\n <Image\n src={listStyleType.match(/\\((.*?)\\)/)[1].replace(/(['\"])/g, '')}\n />\n );\n } else if (ordered) {\n if (lowerAlpha.includes(listStyleType)) {\n bullet = (\n <Text>{orderedAlpha[element.indexOfType].toLowerCase()}.</Text>\n );\n } else if (upperAlpha.includes(listStyleType)) {\n bullet = (\n <Text>{orderedAlpha[element.indexOfType].toUpperCase()}.</Text>\n );\n } else {\n bullet = <Text>{element.indexOfType + 1}.</Text>;\n }\n } else {\n // if (listStyleType.includes('square')) {\n // bullet = <Text>■</Text>;\n // } else {\n bullet = <Text>•</Text>;\n // }\n }\n return (\n <View style={style}>\n {bullet && <View style={bulletStyles}>{bullet}</View>}\n <View style={contentStyles}>{children}</View>\n </View>\n );\n },\n a: ({ style, element, children }) => (\n <Link style={style} src={element.attributes.href}>\n {children}\n </Link>\n ),\n img: ({ style, element }) => {\n const { width, height } = element.attributes;\n const dimensions: any = {};\n if (width) {\n dimensions.width = width;\n }\n if (height) {\n dimensions.height = height;\n }\n const finalStyles = Object.assign({}, ...style, dimensions);\n return (\n <Image\n style={finalStyles}\n source={{\n uri: element.attributes.src,\n body: null,\n method: 'GET',\n headers: {\n 'Cache-Control': 'no-cache',\n 'Access-Control-Allow-Origin': '*',\n },\n }}\n />\n );\n },\n table: ({ element, style, children }) => {\n const tableStyles = element.style.reduce(\n (combined, tableStyle) => Object.assign(combined, tableStyle),\n {} as HtmlStyle\n );\n const { border } = element.attributes as any;\n const overrides: HtmlStyle = {};\n if (\n !(tableStyles as any).borderSpacing ||\n (tableStyles as any).borderCollapse === 'collapse'\n ) {\n overrides.borderLeftWidth = 0;\n overrides.borderTopWidth = 0;\n }\n\n if (border == 0) {\n overrides.borderWidth = '0px';\n overrides.borderStyle = 'none';\n }\n const borderColor = style.find(\n (s) => s.borderColor && s.borderColor !== 'gray'\n )?.borderColor;\n if (borderColor) {\n overrides.borderColor = borderColor;\n }\n const finalStyles = Object.assign({}, ...style, overrides);\n delete finalStyles.height;\n return <View style={finalStyles}>{children}</View>;\n },\n tr: ({ style, children }) => {\n const finalStyles = Object.assign({}, ...style);\n delete finalStyles.height;\n return (\n <View wrap={true} style={finalStyles}>\n {children}\n </View>\n );\n },\n br: ({ style }) => (\n <Text wrap={true} style={style}>\n {'\\n'}\n </Text>\n ),\n td: renderCell,\n th: renderCell,\n};\n\nexport default renderers;\n"]}
|