Skip to content

Commit

Permalink
remove some fn's from export; fix images loading
Browse files Browse the repository at this point in the history
  • Loading branch information
yhdgms1 committed Sep 7, 2021
1 parent 7112765 commit 430a082
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 43 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
52 changes: 12 additions & 40 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import {
descape,
getImageUrlsFromFromHtml,
getUrlsFromCssString,
removeQuotes,
} from './lib'

const serialize = (node: Node) => new XMLSerializer().serializeToString(node)

const base64encode = (str: string): string => {
Expand All @@ -16,32 +23,19 @@ const binaryStringToBase64 = (binaryString: Blob): Promise<string> => {
})
}

const escaped: { [key: string]: string } = {
'&quot;': '"',
'&#39;': "'",
'&amp;': '&',
'&lt;': '<',
'&gt;': '>',
}

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()
Expand Down Expand Up @@ -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,
Expand Down
32 changes: 32 additions & 0 deletions src/lib.ts
Original file line number Diff line number Diff line change
@@ -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 } = {
'&quot;': '"',
'&#39;': "'",
'&amp;': '&',
'&lt;': '<',
'&gt;': '>',
}

export const descape = (string: string) =>
string.replace(/&(quot|#39|amp|lt|gt);/g, match => escaped[match])
2 changes: 1 addition & 1 deletion tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getImageUrlsFromFromHtml,
getUrlsFromCssString,
descape,
} from '../dist/index.js'
} from '../dist/lib.js'

test('removeQuotes', () => {
assert.is(removeQuotes(`'""'`), `""`)
Expand Down
2 changes: 1 addition & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 430a082

Please sign in to comment.