From 9357549afb3f1db84a8564157578168aeeeb9f56 Mon Sep 17 00:00:00 2001 From: Alexandra Thomas <48389444+axthomas29@users.noreply.github.com> Date: Sat, 15 Feb 2020 22:55:50 -0500 Subject: [PATCH] Submitting Multiple Site Showcase Contributions (#21501) * updated sites.yml to add contribution sites * updated sites.yml formatting * fixing TypeError bug for submission --- docs/sites.yml | 23 +++++++++ .../utils/time-to-read.js | 50 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 packages/gatsby-transformer-remark/utils/time-to-read.js diff --git a/docs/sites.yml b/docs/sites.yml index 585dbf5c6832d..d80064bd2fc08 100644 --- a/docs/sites.yml +++ b/docs/sites.yml @@ -9722,3 +9722,26 @@ built_by: Hazem Osama built_by_url: https://github.com/hazem3500 featured: false +- title: Alexandra Thomas + main_url: "https://alexandracthomas.com/" + url: "https://alexandracthomas.com/" + description: > + A portfolio site for Alexandra Thomas, a front-end developer with creative super powers based in Charlotte, NC. + categories: + - Portfolio + - Blog + - Web Development + featured: false +- title: Storto Productions + main_url: "https://www.storto-productions.com/" + url: "https://www.storto-productions.com/about/" + featured: false + description: > + A portfolio site for a video production company based out of Phoenix, AZ. + categories: + - Video + - Blog + - Portfolio + - Business + built_by: Alexandra Thomas + built_by_url: "https://alexandracthomas.com/" diff --git a/packages/gatsby-transformer-remark/utils/time-to-read.js b/packages/gatsby-transformer-remark/utils/time-to-read.js new file mode 100644 index 0000000000000..67e5214091264 --- /dev/null +++ b/packages/gatsby-transformer-remark/utils/time-to-read.js @@ -0,0 +1,50 @@ +"use strict"; + +exports.__esModule = true; +exports.timeToRead = void 0; + +const sanitizeHTML = require(`sanitize-html`); + +const _ = require(`lodash`); // Unicode ranges for Han (Chinese) and Hiragana/Katakana (Japanese) characters + + +const cjRanges = [[11904, 11930], // Han +[11931, 12020], [12032, 12246], [12293, 12294], [12295, 12296], [12321, 12330], [12344, 12348], [13312, 19894], [19968, 40939], [63744, 64110], [64112, 64218], [131072, 173783], [173824, 177973], [177984, 178206], [178208, 183970], [183984, 191457], [194560, 195102], [12353, 12439], // Hiragana +[12445, 12448], [110593, 110879], [127488, 127489], [12449, 12539], // Katakana +[12541, 12544], [12784, 12800], [13008, 13055], [13056, 13144], [65382, 65392], [65393, 65438], [110592, 110593]]; + +function isCjChar(char) { + const charCode = char.codePointAt(0); + return cjRanges.some(([from, to]) => charCode >= from && charCode < to); +} + +const timeToRead = html => { + let timeToRead = 0; + const pureText = sanitizeHTML(html, { + allowTags: [] + }); + const avgWPM = 265; + let latinChars = []; + let cjChars = []; + + for (const char of pureText) { + if (isCjChar(char)) { + cjChars.push(char); + } else { + latinChars.push(char); + } + } // Multiply non-latin character string length by 0.56, because + // on average one word consists of 2 characters in both Chinese and Japanese + + + const wordCount = _.words(latinChars.join(``)).length + cjChars.length * 0.56; + timeToRead = Math.round(wordCount / avgWPM); + + if (timeToRead === 0) { + timeToRead = 1; + } + + return timeToRead; +}; + +exports.timeToRead = timeToRead; \ No newline at end of file