|
@ -7,14 +7,24 @@ const prettyMs = require('pretty-ms'); |
|
|
|
|
|
|
|
|
const getMetaTag = (html, property) => { |
|
|
const getMetaTag = (html, property) => { |
|
|
const regex = new RegExp(`<meta[^>]*property=["|']${property}["|'][^>]*>`, 'i'); |
|
|
const regex = new RegExp(`<meta[^>]*property=["|']${property}["|'][^>]*>`, 'i'); |
|
|
|
|
|
const results = regex.exec(html); |
|
|
|
|
|
|
|
|
return regex.exec(html)[0]; |
|
|
if (!results) { |
|
|
|
|
|
throw new Error(`Missing ${property}`); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return results[0]; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const getMetaTagContent = metaTagHtml => { |
|
|
const getMetaTagContent = metaTagHtml => { |
|
|
const regex = /content=["]([^"]*)["]/i; |
|
|
const contentRegex = /content=["]([^"]*)["]/i; |
|
|
|
|
|
const results = contentRegex.exec(metaTagHtml); |
|
|
|
|
|
|
|
|
|
|
|
if (!results) { |
|
|
|
|
|
throw new Error(`Missing content attribute in ${chalk.bold(metaTagHtml)}`); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return regex.exec(metaTagHtml)[1]; |
|
|
return results[1]; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
module.exports = bundler => { |
|
|
module.exports = bundler => { |
|
@ -23,28 +33,41 @@ module.exports = bundler => { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
console.log(''); |
|
|
console.log(''); |
|
|
|
|
|
|
|
|
const spinner = ora(chalk.grey('Fixing og:image link')).start(); |
|
|
const spinner = ora(chalk.grey('Fixing og:image link')).start(); |
|
|
const start = Date.now(); |
|
|
const start = Date.now(); |
|
|
|
|
|
|
|
|
const htmlPath = path.join(bundler.options.outDir, 'index.html'); |
|
|
const htmlPath = path.join(bundler.options.outDir, 'index.html'); |
|
|
const html = fs.readFileSync(htmlPath).toString(); |
|
|
const html = fs.readFileSync(htmlPath).toString(); |
|
|
|
|
|
let hasErrors = false; |
|
|
|
|
|
|
|
|
const ogImageTag = getMetaTag(html, 'og:image'); |
|
|
try { |
|
|
const ogImageContent = getMetaTagContent(ogImageTag); |
|
|
const ogImageTag = getMetaTag(html, 'og:image'); |
|
|
|
|
|
const ogImageContent = getMetaTagContent(ogImageTag); |
|
|
|
|
|
|
|
|
const ogUrlTag = getMetaTag(html, 'og:url'); |
|
|
const ogUrlTag = getMetaTag(html, 'og:url'); |
|
|
const ogUrlContent = getMetaTagContent(ogUrlTag); |
|
|
const ogUrlContent = getMetaTagContent(ogUrlTag); |
|
|
|
|
|
|
|
|
const absoluteOgImageUrl = url.resolve(ogUrlContent, ogImageContent); |
|
|
const absoluteOgImageUrl = url.resolve(ogUrlContent, ogImageContent); |
|
|
const ogImageTagAbsoluteUrl = ogImageTag.replace(ogImageContent, absoluteOgImageUrl); |
|
|
const ogImageTagAbsoluteUrl = ogImageTag.replace(ogImageContent, absoluteOgImageUrl); |
|
|
const patchedHtml = html.replace(ogImageTag, ogImageTagAbsoluteUrl); |
|
|
const patchedHtml = html.replace(ogImageTag, ogImageTagAbsoluteUrl); |
|
|
|
|
|
|
|
|
fs.writeFileSync(htmlPath, patchedHtml); |
|
|
fs.writeFileSync(htmlPath, patchedHtml); |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
spinner.fail(error.message); |
|
|
|
|
|
|
|
|
|
|
|
hasErrors = true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const end = Date.now(); |
|
|
const end = Date.now(); |
|
|
|
|
|
const symbol = hasErrors ? chalk.red('✖') : '✨ '; |
|
|
|
|
|
const text = hasErrors ? |
|
|
|
|
|
chalk.red('Failed to fix og:image link.') : |
|
|
|
|
|
chalk.green(`Fixed og:image link in ${prettyMs(end - start)}.`); |
|
|
|
|
|
|
|
|
spinner.stopAndPersist({ |
|
|
spinner.stopAndPersist({ |
|
|
symbol: '✨ ', |
|
|
symbol, |
|
|
text: chalk.green(`Fixed og:image link in ${prettyMs(end - start)}.`) |
|
|
text |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}; |
|
|
}; |
|
|