From c3a6f98ee7b96077aba9e3657a8bc51f1a1b9963 Mon Sep 17 00:00:00 2001 From: Zeke Sikelianos Date: Wed, 13 Sep 2017 18:09:31 -0700 Subject: [PATCH] rewrite locale image urls in readmes --- package.json | 1 + script/releases.js | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 312763a100f..7089a2204a0 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "devDependencies": { "chai": "^3.5.0", "check-for-leaks": "^1.0.2", + "cheerio": "^1.0.0-rc.2", "clean-deep": "^2.0.1", "count-array-values": "^1.2.1", "dotenv-safe": "^4.0.4", diff --git a/script/releases.js b/script/releases.js index c70d71f1b42..1661661d15c 100755 --- a/script/releases.js +++ b/script/releases.js @@ -1,6 +1,7 @@ const fs = require('fs') const path = require('path') const github = require('../lib/github') +const cheerio = require('cheerio') const parseGitUrl = require('github-url-to-object') const Duration = require('duration') const downloadExtensions = [ @@ -87,8 +88,25 @@ function go () { latestRelease: false } return github.repos.getReadme(gitHubOptions) - }).then((readme) => { - output[app.slug].readme = readme.data + }).then((response) => { + let readme = response.data + let $ = cheerio.load(readme) + let imagesChanged = false + + $('img').each(function (i, img) { + let currentImg = $(img) + let imageSrc = currentImg.attr('src') + if (imageSrc && imageSrc.indexOf('http') === -1) { + currentImg.attr('src', `${app.repository}/raw/master/${imageSrc}`) + imagesChanged = true + } + }) + if (imagesChanged) { + console.log(`Updating relative image URLs in readme for ${app.name}`) + readme = $('body').html() + } + + output[app.slug].readme = readme go() }) }