Skip to content

Commit

Permalink
Don't store fonts and increase scale of watermarks
Browse files Browse the repository at this point in the history
  • Loading branch information
ashley-hebler committed Jan 4, 2021
1 parent bd7e575 commit 78f0f51
Show file tree
Hide file tree
Showing 27 changed files with 66 additions and 20 deletions.
Binary file removed assets/fonts/OpenSans-Bold.ttf
Binary file not shown.
Binary file modified assets/images/watermarks/1999.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2006.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2007.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2008.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2009.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2010.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2011.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2012.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2013.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2014.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2015.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2016.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2017.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2018.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2019.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/watermarks/2020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/watermarks/2021.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 38 additions & 4 deletions docs/config/tasks/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const axios = require('axios');
const fs = require('fs-extra');
const nunjucks = require('nunjucks');
const path = require('path');
const {
docsStyles,
siteURL,
mappedGithubData,
buildDir,
} = require('../paths.js');

const generateClassName = (str) => {
Expand Down Expand Up @@ -85,13 +88,15 @@ const slugify = (text) =>

const stripTags = (str) => str.replace(/(<([^>]+)>)/gi, '');

const getKeywords = obj => {
const {name, description, modifiers} = obj;
const getKeywords = (obj) => {
const { name, description, modifiers } = obj;
const className = generateClassName(name);
const keywordsStr = /Keywords: (.*)/;
const keywordsMatch = keywordsStr.exec(description);
let keywords = [`${name.toLowerCase()}`, className];
const modifierNames = modifiers.map((modifier) => stripSelector(modifier.data.name));
const modifierNames = modifiers.map((modifier) =>
stripSelector(modifier.data.name)
);
if (modifiers.length > 0) {
keywords = [...keywords, ...modifierNames];
}
Expand All @@ -103,7 +108,7 @@ const getKeywords = obj => {
keywords = [...labeledKeywords, ...keywords];
}
return keywords;
}
};
const getDetails = (description, name) => {
const isHelperStr = '{{isHelper}}';
const isHelper = description.includes(isHelperStr);
Expand Down Expand Up @@ -149,6 +154,34 @@ const buildTokenArr = (arr) =>
.map((arrMap) => arrMap.list.map((token) => ({ ...token })))
.reduce((acc, val) => acc.concat(val), []);

const downloadAsset = async (url, name) => {
const filepath = path.resolve(buildDir, name);
const writer = fs.createWriteStream(filepath);

const response = await axios({
url,
method: 'GET',
responseEncoding: 'binary',
responseType: 'stream',
});

await new Promise((resolve, reject) => {
response.data.pipe(writer);
let error = null;
writer.on('error', (err) => {
error = err;
writer.close();
reject(err);
});
writer.on('close', () => {
if (!error) {
resolve(true);
}
});
});
return filepath;
};

module.exports = {
findUsageInfo,
generateClassName,
Expand All @@ -163,4 +196,5 @@ module.exports = {
convertArrayToObject,
buildTokenArr,
getKeywords,
downloadAsset,
};
28 changes: 20 additions & 8 deletions docs/config/tasks/watermark.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { docsImages } from '../paths';
import { downloadAsset } from './utils';

const { createCanvas, Image, registerFont } = require('canvas');
const fs = require('fs-extra');
const ora = require('ora');
const path = require('path');

const bugHeight = 35;
const bugWidth = 30;
const bugHeight = 70;
const bugWidth = 60;
const bug = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 174.9 200" width="${bugWidth}" height="${bugHeight}"><path fill="#ffc200" d="M0 0v200l40.2-25.1h134.6V0H0zm125.2 139.7l-38.3-25.2-38.3 25.2 12.1-44.2L25 66.8l45.8-2.1L87 21.8l16.2 42.9 45.8 2.1-35.8 28.6 12 44.3z"/></svg>`;
registerFont('./assets/fonts/OpenSans-Bold.ttf', {
family: 'Open Sans Bold',
});

interface Watermark {
year: Number;
Expand All @@ -19,15 +17,27 @@ interface Watermark {

const watermarkDir = path.join(docsImages, 'watermarks', '/');

async function initFonts(): Promise<any> {
// remote fonts must be downloaded
// https://github.com/Automattic/node-canvas/issues/1527
const fontPath = await downloadAsset(
'https://s3.amazonaws.com/cdn.texastribune.org/fonts/OpenSans-Bold.ttf',
'fonts/OpenSans-Bold.ttf'
);
registerFont(fontPath, {
family: 'Open Sans Bold',
});
}

async function createWatermark(year: Number): Promise<Watermark> {
const width = 200;
const height = 50;
const width = 330;
const height = 90;

const canvas = createCanvas(width, height);
const context = canvas.getContext('2d');
context.fillStyle = '#222';
context.fillRect(0, 0, width, height);
context.font = 'bold 16px Open Sans Bold';
context.font = 'bold 32px Open Sans Bold';
context.fillStyle = '#fff';
const text = `Published ${year}`;
context.textBaseline = 'middle';
Expand All @@ -48,6 +58,8 @@ async function createWatermark(year: Number): Promise<Watermark> {

async function build() {
const spinner = ora('Generating watermark images').start();
// download and register CDN fonts
await initFonts();
const now = new Date().getUTCFullYear();
const years = Array(now - 1998)
.fill('')
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 78f0f51

Please sign in to comment.