From 29184a2b3aa6cec1fff6e053ba846e5b097e3373 Mon Sep 17 00:00:00 2001 From: "Kim, Jang-hwan" Date: Fri, 22 Apr 2022 03:59:31 +0900 Subject: [PATCH] Fix incorrect image copies [#408] (#411) * Fix copying every images that looks like tag Instead of doing regex search on markdown source, (both markdown `![alt](/img/src/path "title")]` and html ``) convert it to html first and then search only for html. This fixes the problem of falsely trying to copy img tags that are not actual images, but codes: ```html ``` which is HTML-escaped and is safe from being caught by `htmlImageRe`. * Fix image copy failure from throwing errors during static It should just warn and continue. --- lib/static.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/static.js b/lib/static.js index bf07a0e..d4b1506 100644 --- a/lib/static.js +++ b/lib/static.js @@ -3,7 +3,7 @@ const fs = require('fs-extra'); const path = require('path'); const _ = require('lodash'); const { getOptions, getAssetsDir, getPath, getStaticDir, getSlideOptions, getFilesGlob } = require('./config'); -const { isDirectory, isFile, parseYamlFrontMatter, getFilePaths, isAbsoluteURL } = require('./util'); +const { md, isDirectory, isFile, parseYamlFrontMatter, getFilePaths, isAbsoluteURL } = require('./util'); const { revealBasePath, highlightThemePath } = require('./constants'); const { renderFile } = require('./render'); const { renderListFile } = require('./listing'); @@ -11,7 +11,6 @@ const featuredSlide = require('./featured-slide'); const files = new Set(); -const mdImageRE = /!\[[^\]]*\]\((.*?)\s*(["'](?:.*[^"'])["'])?\s*\)/g; const htmlImageRE = //g; const relativeDir = (from, to) => path.relative(from, to).replace(/^\.\./, '.'); @@ -63,13 +62,14 @@ const copyAssetsAndWriteFile = async (sourceDir, file, targetDir) => { const awaits = await copyAssetsFromOptions(markdown); const base = relativeDir(file, '.'); const markup = await renderFile(path.join(sourceDir, file), { base }); - let image; - while ((image = mdImageRE.exec(markdown) || htmlImageRE.exec(markdown))) { + let image; + const html = md.marked(markdown.toString()); + while (image = htmlImageRE.exec(html)) { const [, imgPath] = image; if (!isAbsoluteURL(imgPath)) { const relPath = path.join(path.dirname(file), imgPath); - awaits.push(cp(path.join(sourceDir, relPath), path.join(targetDir, relPath))); + awaits.push(cp(path.join(sourceDir, relPath), path.join(targetDir, relPath)).catch((err) => console.warn(err))); } }