Skip to content

Commit

Permalink
fix: regnerate instead of just insert
Browse files Browse the repository at this point in the history
  • Loading branch information
Chun-Yang committed Jul 22, 2016
1 parent 2f3453b commit a1e2e8e
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions lib/update/copy-fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,32 @@ const readOneHtml = require('../helpers/read-one-html');
const fs = require('fs');
const regenerateFolderInPublic = require('../helpers/regenerate-folder-in-public');

function copyFontScriptsToMainHtml() {
// get font script tags from head of a html file
const $from = cheerio.load(readOneHtml());
$from('head').children().each((index, element) => {
const src = $from(element).attr('src');
function removeFontScriptTags($, inverse) {
$('head').children().each((index, element) => {
const src = $(element).attr('src');
const isWebFontJs = src && _.endsWith(src, 'webfont.js');
if (isWebFontJs) {
return;
}

const content = _.trim($from(element).text());
const content = _.trim($(element).text());
const isWebFontLoad = content && _.startsWith(content, 'WebFont.load');
if (isWebFontLoad) {
return;
}

$from(element).remove();
const isFontTag = (isWebFontJs || isWebFontLoad);
const shouldRemove = inverse ? !isFontTag : isFontTag;

if (shouldRemove) {
$(element).remove();
}
});
}

function copyFontScriptsToMainHtml() {
// get font script tags from head of a html file
const $from = cheerio.load(readOneHtml());
removeFontScriptTags($from, true);
const tagsString = _.trim($from('head').html());

// insert script tags into the the main.html
const $to = cheerio.load(fs.readFileSync('client/main.html', 'utf-8'));
removeFontScriptTags($to);
$to('head').append(tagsString);
fs.writeFileSync('client/main.html', $to.html());
}
Expand Down

0 comments on commit a1e2e8e

Please sign in to comment.