Fix PUBLIC_URL bug referencing images in md files

This commit is contained in:
dennis.ludwig
2021-06-25 15:21:38 +02:00
parent 944fd04928
commit cc217cffb6
2 changed files with 31 additions and 27 deletions

View File

@@ -117,6 +117,7 @@ var checkAttributes = function (fileName, attributes) {
return attributes;
};
var createRevisionsFromFiles = function (fileNames) {
var publicUrl = process.env.PUBLIC_URL;
return Promise.all(fileNames.map(function (fileName) {
return new Promise(function (resolve, reject) {
fs_extra_1.readFile(fileName, "utf8", function (err, data) {
@@ -127,7 +128,7 @@ var createRevisionsFromFiles = function (fileNames) {
var fm = front_matter_1.default(data);
// add target attribute to external links
// todo: check path
var html = marked_1.default(fm.body.replace(/\]\(\//g, "](/techradar/"));
var html = marked_1.default(fm.body.replace(/\]\(\//g, "](" + publicUrl + "/"));
html = html.replace(/a href="http/g, 'a target="_blank" rel="noopener noreferrer" href="http');
resolve(__assign(__assign(__assign({}, itemInfoFromFilename(fileName)), checkAttributes(fileName, fm.attributes)), { fileName: fileName, body: html }));
}

View File

@@ -44,34 +44,37 @@ const checkAttributes = (fileName: string, attributes: FMAttributes) => {
return attributes;
};
const createRevisionsFromFiles = (fileNames: string[]) =>
Promise.all(
fileNames.map((fileName) => {
return new Promise<Revision>((resolve, reject) => {
readFile(fileName, "utf8", (err, data) => {
if (err) {
reject(err);
} else {
const fm = frontMatter<FMAttributes>(data);
// add target attribute to external links
// todo: check path
let html = marked(fm.body.replace(/\]\(\//g, "](/techradar/"));
html = html.replace(
/a href="http/g,
'a target="_blank" rel="noopener noreferrer" href="http'
);
const createRevisionsFromFiles = (fileNames: string[]) => {
const publicUrl = process.env.PUBLIC_URL;
return Promise.all(
fileNames.map(
(fileName) =>
new Promise<Revision>((resolve, reject) => {
readFile(fileName, "utf8", (err, data) => {
if (err) {
reject(err);
} else {
const fm = frontMatter<FMAttributes>(data);
// add target attribute to external links
// todo: check path
let html = marked(fm.body.replace(/\]\(\//g, `](${publicUrl}/`));
html = html.replace(
/a href="http/g,
'a target="_blank" rel="noopener noreferrer" href="http'
);
resolve({
...itemInfoFromFilename(fileName),
...checkAttributes(fileName, fm.attributes),
fileName,
body: html,
} as Revision);
}
});
});
})
resolve({
...itemInfoFromFilename(fileName),
...checkAttributes(fileName, fm.attributes),
fileName,
body: html,
} as Revision);
}
});
})
)
);
};
const itemInfoFromFilename = (fileName: string) => {
const [release, name] = fileName.split(path.sep).slice(-2);