diff --git a/package.json b/package.json index 21ed75b..306251a 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "description": "Use SVG foreignObject to render images of HTML content", "author": "Artem Schukin", "license": "MIT", - "version": "0.1.1", + "version": "0.1.2", "type": "module", "homepage": "https://github.com/Artemis69/svg-foreignobject-screenshot", "repository": { diff --git a/src/index.ts b/src/index.ts index fe7a647..2dcffa6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,10 @@ +import { + descape, + getImageUrlsFromFromHtml, + getUrlsFromCssString, + removeQuotes, +} from './lib' + const serialize = (node: Node) => new XMLSerializer().serializeToString(node) const base64encode = (str: string): string => { @@ -16,32 +23,19 @@ const binaryStringToBase64 = (binaryString: Blob): Promise => { }) } -const escaped: { [key: string]: string } = { - '"': '"', - ''': "'", - '&': '&', - '<': '<', - '>': '>', -} - -export const descape = (string: string) => - string.replace(/&(quot|#39|amp|lt|gt);/g, match => escaped[match]) - const getResourceAsBase64 = async ( url: string ): Promise<{ url: string base64: string }> => { - url = descape(url) - try { const res = await fetch( - url.startsWith('http') ? 'https://images.weserv.nl/?url=' + url : url, - { - mode: 'cors', - credentials: 'omit', - } + url.startsWith('http://') || + url.startsWith('https://') || + url.startsWith('//') + ? 'https://images.weserv.nl/?url=' + encodeURIComponent(descape(url)) + : descape(url) ) const blob = await res.blob() @@ -70,28 +64,6 @@ const getMultipleResourcesAsBase64 = ( return Promise.all(promises) } -export const getImageUrlsFromFromHtml = (html: string): string[] => { - const urls = Array.from( - html.matchAll(/<(?:img|image).*?(?:href|src)=(["|'])(.*?)(\1)/gm) - ) - .map(match => match[2]) - .filter( - url => !url.startsWith('data:') && !url.startsWith('#') && url !== '' - ) - return urls -} - -export const getUrlsFromCssString = (cssRuleString: string): string[] => { - const urls = Array.from(cssRuleString.matchAll(/url\((.*?)\)/gi)) - .map(match => removeQuotes(match[1])) - .filter(url => !url.startsWith('data:') && url !== '') - - return urls -} - -export const removeQuotes = (str: string) => - str.replace(/^("|').*?(\1)$/gm, match => match.slice(1, -1)) - export const buildSvgDataURI = async ( contentHtml: string, width: number, diff --git a/src/lib.ts b/src/lib.ts new file mode 100644 index 0000000..f9b7d05 --- /dev/null +++ b/src/lib.ts @@ -0,0 +1,32 @@ +export const getImageUrlsFromFromHtml = (html: string): string[] => { + const urls = Array.from( + html.matchAll(/<(?:img|image).*?(?:href|src)=(["|'])(.*?)(\1)/gm) + ) + .map(match => match[2]) + .filter( + url => !url.startsWith('data:') && !url.startsWith('#') && url !== '' + ) + return urls +} + +export const getUrlsFromCssString = (cssRuleString: string): string[] => { + const urls = Array.from(cssRuleString.matchAll(/url\((.*?)\)/gi)) + .map(match => removeQuotes(match[1])) + .filter(url => !url.startsWith('data:') && url !== '') + + return urls +} + +export const removeQuotes = (str: string) => + str.replace(/^("|').*?(\1)$/gm, match => match.slice(1, -1)) + +const escaped: { [key: string]: string } = { + '"': '"', + ''': "'", + '&': '&', + '<': '<', + '>': '>', +} + +export const descape = (string: string) => + string.replace(/&(quot|#39|amp|lt|gt);/g, match => escaped[match]) diff --git a/tests/main.js b/tests/main.js index d107210..9655b0e 100644 --- a/tests/main.js +++ b/tests/main.js @@ -5,7 +5,7 @@ import { getImageUrlsFromFromHtml, getUrlsFromCssString, descape, -} from '../dist/index.js' +} from '../dist/lib.js' test('removeQuotes', () => { assert.is(removeQuotes(`'""'`), `""`) diff --git a/tsup.config.ts b/tsup.config.ts index ec94582..63ee638 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -3,7 +3,7 @@ import type { Options } from 'tsup' export const tsup: Options = { sourcemap: false, clean: true, - entryPoints: ['src/index.ts'], + entryPoints: ['src/index.ts', 'src/lib.ts'], target: 'node16', format: ['esm', 'cjs'], dts: true,