Hexo is a fast, simple and powerful blog framework. You write posts in Markdown (or other markup languages) and Hexo generates static files with a beautiful theme in seconds.
let imageReg = /!\[(?<alt>[^\]]*)\]\((?<url>.*?)(?=\"|\))(?<title>\".*\")?\)/g; let cwd = process.cwd();
let hexo_img_locator = function (data) { let postPath = data.full_source; if (!postPath.endsWith('.md')) { printInfo(`skip ${postPath}`); return data; } let mapping = newMap(); let cannotAccess = newArray();
let match = null; while ((match = imageReg.exec(data.content)) != null) { let imageUrl = match.groups.url; // skip online image and local file with absolute path if (imageUrl.startsWith('http://') || imageUrl.startsWith('https://') || imageUrl.startsWith('/')) { continue; } let imageAbsPath = path.resolve(path.dirname(postPath), imageUrl);
if (!fs.existsSync(imageAbsPath)) { cannotAccess.push(imageUrl); continue; } elseif (cannotAccess.length > 0) { // no need process any more continue; }
let imageAbsPathFromSource = path.sep + path.relative(`${cwd}${path.sep}source`, imageAbsPath); let imageDirective = match[0]; mapping.set(imageDirective, imageDirective.replace(imageUrl, imageAbsPathFromSource)); printInfo(`[hexo-img-locator] ${imageUrl} -> ${imageAbsPathFromSource}`); }
if (cannotAccess.length != 0) { printError(`[hexo-img-locator] cannot access ${cannotAccess[0]} and other ${cannotAccess.length - 1} from ${path.relative(cwd, data.full_source)}, file not found.`) process.exit(1) } mapping.forEach((v, k, map) => { data.content = data.content.replace(k, v); }) return data; }