Skip to content

Commit

Permalink
Fix incorrect image copies [#408] (#411)
Browse files Browse the repository at this point in the history
* Fix copying every images that looks like <img> tag

Instead of doing regex search on markdown source,
(both markdown `![alt](/img/src/path "title")]` and html `<img src="/path" />`)
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
<img src="please/dont/bother/copying" />
```

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.
  • Loading branch information
jangxyz authored Apr 21, 2022
1 parent 289f18e commit 29184a2
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ 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');
const featuredSlide = require('./featured-slide');

const files = new Set();

const mdImageRE = /!\[[^\]]*\]\((.*?)\s*(["'](?:.*[^"'])["'])?\s*\)/g;
const htmlImageRE = /<img.+src=["'](.+?)["'](?:.+?)>/g;

const relativeDir = (from, to) => path.relative(from, to).replace(/^\.\./, '.');
Expand Down Expand Up @@ -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)));
}
}

Expand Down

0 comments on commit 29184a2

Please sign in to comment.