Skip to content

Commit

Permalink
fix: embedded file generalization fixed - alt used (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
akosbalasko authored Jan 6, 2024
1 parent 6510575 commit 9930d6e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 43 deletions.
58 changes: 19 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 19 additions & 3 deletions src/process-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,17 @@ export const extractDataUrlResources = (

const {absoluteResourceWorkDir, relativeResourceWorkDir} = getResourceWorkDirs(note);
fsExtra.mkdirsSync(absoluteResourceWorkDir);

// change links hrefs
content = content.replace(/ (href)="data:([^;,]*)(;base64)?,([^"]*)"(.*?)<\/a>/g, (match,argument, mediatype, encoding, data, postfix) => {
const fileName = createResourceFromData(argument, match, mediatype, encoding === ';base64', data, absoluteResourceWorkDir, note);
const argValue = `${relativeResourceWorkDir}${yarleOptions.pathSeparator}${fileName}`;

return `yarle-file-resource="true" ${argument}="${argValue}"${postfix}`;
});
// src|href="data:image/svg+xml;base64,..." --> src="resourceDir/fileName"
return content.replace(/([^= ]*)="data:([^;,]*)(;base64)?,([^"]*)"/g, (match,argument, mediatype, encoding, data) => {
const fileName = createResourceFromData(mediatype, encoding === ';base64', data, absoluteResourceWorkDir, note);
return content.replace(/(src)="data:([^;,]*)(;base64)?,([^"]*)"/g, (match,argument, mediatype, encoding, data) => {
const fileName = createResourceFromData(argument, match, mediatype, encoding === ';base64', data, absoluteResourceWorkDir, note);
const argValue = `${relativeResourceWorkDir}${yarleOptions.pathSeparator}${fileName}`;

return `yarle-file-resource="true" ${argument}="${argValue}"`;
Expand All @@ -122,6 +129,8 @@ export const extractDataUrlResources = (

// returns filename of new resource
const createResourceFromData = (
argument: string,
match: string,
mediatype: string,
base64: boolean,
data: string,
Expand All @@ -131,7 +140,14 @@ const createResourceFromData = (
const baseName = 'embedded'; // data doesn't seem to include useful base filename
const extension = extensionForMimeType(mediatype) || '.dat';
const index = utils.getFileIndex(absoluteResourceWorkDir, baseName);
const fileName = index < 1 ? `${baseName}.${extension}` : `${baseName}.${index}.${extension}`;
let fileName = index < 1 ? `${baseName}.${extension}` : `${baseName}.${index}.${extension}`;


if (argument === 'href'){
const baseNameCandidate = match.split("alt=\"")[1]?.split("\"")[0]
if (baseNameCandidate)
fileName = baseNameCandidate
}
const absFilePath = `${absoluteResourceWorkDir}${path.sep}${fileName}`;

if (!base64 && !data.startsWith('&lt;svg')) { // TODO: handle <svg, tags, they may be checkboxes
Expand Down
Loading

0 comments on commit 9930d6e

Please sign in to comment.