1
0
mirror of https://github.com/GenderDysphoria/GenderDysphoria.fyi.git synced 2025-11-25 20:42:40 +00:00

Moved new build process out of gulp folder

This commit is contained in:
Jocelyn Badgley (Twipped)
2020-02-28 10:33:39 -08:00
parent e80428ec2b
commit 56d5f33453
25 changed files with 4 additions and 6 deletions

3
build/lib/dimensions.js Normal file
View File

@@ -0,0 +1,3 @@
var { promisify } = require('util');
module.exports = exports = promisify(require('image-size'));

4
build/lib/glob.js Normal file
View File

@@ -0,0 +1,4 @@
var { promisify } = require('util');
const glob = require('glob');
module.exports = exports = promisify(glob);

View File

@@ -0,0 +1,181 @@
module.exports = exports = function (md, options) {
options = {
fence: '|||',
...options,
};
function debug (...args) {
if (options.debug) console.log(...args); // eslint-disable-line
}
const fenceLen = options.fence.length;
// const fenceFirst = options.fence.charCodeAt(0);
function scanAhead (state, line, pos) {
const position = state.src.indexOf(options.fence, pos);
if (position === -1) {
// there are no html blocks in this entire file
state.discreteHtmlScan = {
present: false,
};
return false;
}
while (position > state.eMarks[line]) {
line++;
}
state.discreteHtmlScan = {
present: true,
position,
line,
};
return true;
}
md.block.ruler.before('fence', 'raw', (state, startLine, lastLine) => {
let pos = state.bMarks[startLine] + state.tShift[startLine];
let endOfLine = state.eMarks[startLine];
// if we have yet to do a scan of this file, perform one.
if (!state.discreteHtmlScan && !scanAhead(state, startLine, pos)) {
debug('First scan, nothing found');
return false;
}
if (!state.discreteHtmlScan.present) {
debug('Have scanned, did not find');
return false;
}
// add one to the line here in case there is a line break in a paragraph.
if (state.discreteHtmlScan.line > startLine + 1) {
debug('Have scanned, found, but after this line', { startLine, targetLine: state.discreteHtmlScan.line });
return false;
}
if (startLine > state.discreteHtmlScan.line) {
// we dun fucked up
debug('We somehow got ahead of ourselves', { startLine, line: state.discreteHtmlScan.line, lastLine, pos, endOfLine, tokens: state.tokens });
throw new Error('markdown-it-discrete-html encountered a parsing error.');
}
// at this point we should be on a line that contains a fence mark
debug({ l: 67, startLine, scan: state.discreteHtmlScan });
let openIndex, closer, nextLine;
openIndex = state.discreteHtmlScan.position;
do {
let token, closeIndex;
const tokens = [];
const preBlock = openIndex > pos && state.src.slice(pos, openIndex);
debug({ l: 75, preBlock, startLine, lastLine });
openIndex += fenceLen;
pos = openIndex;
if (preBlock && !!preBlock.trim()) {
md.block.parse(preBlock, md, state.env, tokens);
switch (tokens[tokens.length - 1].type) {
case 'heading_close':
case 'paragraph_close':
closer = tokens.pop();
// fallthrough
default:
state.tokens.push(...tokens);
}
}
debug({ l: 92, tokens });
// find terminating fence
if (!scanAhead(state, startLine, pos)) {
debug({ l: 96, remaining: state.src.slice(pos) });
// console.error(state.src)
throw new Error(`Could not find terminating "${options.fence}" for a raw html block.`);
}
closeIndex = state.discreteHtmlScan.position;
nextLine = state.discreteHtmlScan.line;
if (nextLine === startLine) nextLine++;
endOfLine = state.eMarks[nextLine];
const content = state.src.substring(openIndex, closeIndex);
closeIndex += fenceLen;
pos = closeIndex;
if (content.trim()) {
token = state.push(closer ? 'html_inline' : 'html_block', '', 0);
token.map = [ startLine, nextLine ];
token.content = content;
token.block = true;
debug({ l: 115, tokens: [ token ], nextLine, pos, endOfLine: state.eMarks[nextLine], len: state.src.length, remaining: state.src.slice(pos) }); // eslint-disable-line
}
if (pos === endOfLine) {
// we have ended this line, nothing more to do here.
if (closer) {
state.tokens.push(closer);
debug({ l: 122, tokens: [ closer ] });
}
state.discreteHtmlScan = null;
state.line = nextLine + 1;
return true;
}
// still more left in this line, see if there is another block
if (scanAhead(state, nextLine, pos)) {
// we found another block, but it isn't on this line, so break out.
if (state.discreteHtmlScan.line > nextLine) {
if (closer) {
state.tokens.push(closer);
debug({ l: 135, tokens: [ closer ] });
}
state.line = nextLine + 1;
return true;
}
// next block is on this line, grab everything between here and there
openIndex = state.discreteHtmlScan.position;
} else {
// no more blocks on this line, grab everything between here and the end of the line
openIndex = endOfLine;
}
debug({ l: 147, pos, openIndex, remaining: state.src.slice(pos) });
const postBlock = state.src.slice(pos, openIndex);
token = null;
if (postBlock.trim()) {
token = state.push('inline', '', 0);
token.content = postBlock;
token.map = [ nextLine, nextLine ];
token.children = [];
tokens.push(token);
}
debug({ l: 158, tokens: [ token ], postBlock, pos, openIndex, closeIndex, endOfLine });
pos = openIndex;
startLine = nextLine + 1;
endOfLine = state.eMarks[startLine];
debug({ l: 164, pos, startLine, endOfLine, remaining: state.src.slice(pos) });
} while (pos + fenceLen < endOfLine);
if (closer) {
state.tokens.push(closer);
debug({ l: 169, tokens: [ closer ] });
}
openIndex += fenceLen;
pos = openIndex;
state.line = startLine;
return true;
});
};

View File

@@ -0,0 +1,46 @@
const { flatten } = require('lodash');
module.exports = exports = function (md) {
md.core.ruler.push(
'modify-token',
(state) => {
state.tokens = flatten(state.tokens.map(descend).filter(Boolean));
return false;
},
);
};
function descend (token) {
switch (token.type) {
case 'link_open':
case 'link_close':
case 'html_block':
return false;
case 'heading_open':
token.type = 'paragraph_open';
token.tag = 'p';
token.markup = '';
return token;
case 'heading_close':
token.type = 'paragraph_close';
token.tag = 'p';
token.markup = '';
return token;
case 'image':
case 'container':
return token.children;
default:
if (token.children && token.children.length) {
token.children = flatten(token.children.map(descend).filter(Boolean));
}
return token;
}
}

47
build/lib/random.js Normal file
View File

@@ -0,0 +1,47 @@
'use strict';
var crypto = require('crypto');
var uuid = require('uuid').v4;
// based on code from http://stackoverflow.com/a/25690754/110189
function randomString (length, chars) {
if (!chars) {
throw new Error('Argument \'chars\' is undefined');
}
var charsLength = chars.length;
if (charsLength > 256) {
throw new Error('Length must be less than 256 characters');
}
var randomBytes = crypto.randomBytes(length);
var result = new Array(length);
var cursor = 0;
for (var i = 0; i < length; i++) {
cursor += randomBytes[i];
result[i] = chars[cursor % charsLength];
}
return result.join('');
}
module.exports = exports = function (min, max) {
if (Array.isArray(min)) return exports.from(min);
if (typeof max === 'undefined') {
if (min > 0) {
max = min;
min = 0;
} else {
max = 0;
}
}
return Math.floor((Math.random() * (max - min + 1)) + min);
};
exports.alphanumeric = (length) => randomString(length, 'ABCDEFGHIJKLMNOPQRSTUWXYZ0123456789');
exports.alpha = (length) => randomString(length, 'ABCDEFGHIJKLMNOPQRSTUWXYZ');
exports.fromCharSet = randomString;
exports.from = (array) => array[exports(array.length - 1)];
exports.id = (length) => uuid().replace(/-/g, '').substr(0, length);

140
build/lib/tweetparse.js Normal file
View File

@@ -0,0 +1,140 @@
var twemoji = require('twemoji' );
const { deepPick, has } = require('./util');
const schema = {
id_str: true,
created_at: true,
user: {
screen_name: true,
avatar: true,
name_html: true,
verified: true,
protected: true,
},
html: true,
quoted_status: {
user: {
screen_name: true,
avatar: true,
name_html: true,
verified: true,
protected: true,
},
},
entities: { media: [ {
type: true,
media_url_https: true,
video_info: { variants: [ {
url: true,
content_type: true,
} ] },
} ] },
media: true,
};
var entityProcessors = {
hashtags (tags, tweet) {
tags.forEach((tagObj) => {
tweet.html = tweet.html.replace('#' + tagObj.text, `<a href="https://twitter.com/hashtag/{tagObj.text}" class="hashtag">#${tagObj.text}</a>`);
});
},
symbols (/* symbols, tweet */) {
},
user_mentions (users, tweet) {
users.forEach((userObj) => {
var regex = new RegExp('@' + userObj.screen_name, 'gi' );
tweet.html = tweet.html.replace(regex, `<a href="https://twitter.com/${userObj.screen_name}" class="mention">@${userObj.screen_name}</a>`);
});
},
urls (urls, tweet) {
urls.forEach((urlObj) => {
var quotedTweetHtml = '';
var indices = urlObj.indices;
var urlToReplace = (tweet.full_text || tweet.text).substring(indices[0], indices[1]);
var finalText = quotedTweetHtml || urlObj.display_url.link(urlObj.expanded_url);
tweet.html = tweet.html.replace(urlToReplace, finalText);
});
},
media (media, tweet) {
media.forEach((mediaObj) => {
tweet.html = tweet.html.replace(mediaObj.url, '');
return;
// if (mediaObj.type === 'photo') {
// // Use HTTPS if available
// var src = mediaObj.media_url_https ? mediaObj.media_url_https : mediaObj.media_url;
// if (options &&
// options.photoSize &&
// mediaObj.sizes &&
// mediaObj.sizes[options.photoSize]) {
// // If specified size is available, patch image src to use it
// src = src + ':' + options.photoSize;
// }
// tweet.html = tweet.html.replace(mediaObj.url, `<img src="${src}" alt=""/>`);
// } else if (mediaObj.type === 'video') {
// var source = '';
// mediaObj.video_info.variants.forEach((info) => {
// source += `<source src="${info.url}" type="${info.content_type}">`;
// });
// var video = `<video controls poster="${mediaObj.media_url}">${source}</video>`;
// tweet.html = tweet.html.replace(mediaObj.url, video);
// }
});
},
};
module.exports = exports = function (tweets) {
return tweets.length ? tweets.map(parseTweet) : parseTweet(tweets);
function parseTweet (tweet) {
// clone the tweet so we're not altering the original
tweet = JSON.parse(JSON.stringify(tweet));
tweet.user.avatar = {
input: tweet.user.profile_image_url_https,
output: 'tweets/' + tweet.user.screen_name + '.jpg',
};
tweet.media = [
tweet.user.avatar,
];
// Copying text value to a new property html. The final output will be set to this property
tweet.html = (tweet.full_text || tweet.text)
.split(/(\r\n|\n\r|\r|\n)+/)
.map((s) => s.trim() && '<p>' + s + '</p>')
.filter(Boolean)
.join('');
if (tweet.quoted_status) {
tweet.quoted_status = parseTweet(tweet.quoted_status);
}
if (has(tweet, 'entities.media') && has(tweet, 'extended_entities.media')) {
tweet.entities.media = tweet.extended_entities.media;
delete tweet.extended_entities;
}
// Process entities
if (Object.getOwnPropertyNames(tweet.entities).length) {
for (let [ entityType, entity ] of Object.entries(tweet.entities)) { // eslint-disable-line prefer-const
entityProcessors[entityType](entity, tweet);
}
}
// Process Emoji's
tweet.html = twemoji.parse(tweet.html);
tweet.user.name_html = twemoji.parse(tweet.user.name);
return deepPick(tweet, schema);
}
};

1654
build/lib/util.js Normal file

File diff suppressed because it is too large Load Diff