Skip to content

Commit

Permalink
social microformats updated
Browse files Browse the repository at this point in the history
  • Loading branch information
SilentImp committed Jun 14, 2020
1 parent 0e311bd commit 0c80455
Show file tree
Hide file tree
Showing 31 changed files with 686 additions and 71 deletions.
118 changes: 112 additions & 6 deletions package-lock.json

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

14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
{
"name": "@frontender-magazine/builder",
"version": "1.0.0",
"version": "2.0.0",
"description": "build an article from url",
"main": "index.js",
"main": "source/index.js",
"scripts": {
"precommit": "lint-staged",
"lint": "eslint --color -f stylish --fix ./source/linttest/*.jsx"
"lint": "eslint --color -f stylish --fix ./source/linttest/*.jsx",
"postversion": "git push && git push --tags",
"major": "npm version major && npm publish --tag latest --access public",
"minor": "npm version minor && npm publish --tag latest --access public",
"patch": "npm version patch && npm publish --tag latest --access public",
"dopreminor": "npm version preminor && npm publish --tag next --access public",
"dopremajor": "npm version premajor && npm publish --tag next --access public",
"doprepatch": "npm version prepatch && npm publish --tag next --access public"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -73,6 +80,7 @@
"joi": "^14.3.1",
"jsdom": "^16.2.2",
"keyword-extractor": "0.0.19",
"open-graph-scraper": "^3.6.2",
"path": "^0.12.7",
"prettier": "^2.0.5",
"pretty": "^2.0.0",
Expand Down
52 changes: 39 additions & 13 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ const path = require('path');
const fs = require('fs');
const { flatten } = require('array-flatten');

// console.log(flatten);

// process.exit(0);

dotenv.config();

// eslint-disable-next-line no-unused-vars
const logger = (name) => {
console.log(name);
return name;
};

/**
* ArticleBuilder
* @class
Expand Down Expand Up @@ -66,20 +68,20 @@ class ArticleBuilder {
};
let plugins = this.pluginCollector(path.resolve('./source/plugins'));
// eslint-disable-next-line import/no-dynamic-require, global-require
plugins = plugins.map(uri => (require(uri)));
plugins = plugins.map((uri) => (require(uri)));

await flatten(this.stages
return flatten(this.stages
// remove stages we should skip
.filter(stage => (!this.skip.stages.includes(stage)))
.filter((stage) => (!this.skip.stages.includes(stage)))
// map stages to plugins array
.map(stage => plugins
.map((stage) => plugins
// filter plugin that have no functions for this stage
.filter(plugin => ((plugin[stage] !== undefined) && (typeof plugin[stage] === 'function')))
.filter((plugin) => ((plugin[stage] !== undefined) && (typeof plugin[stage] === 'function')))
// filter plugin we need to skip
.filter((plugin) => {
const { meta: { name } } = plugin;
return (this.skip.plugins.find(
skippedPlugin => (
(skippedPlugin) => (
(
skippedPlugin.name === name
&& skippedPlugin.stages === undefined
Expand All @@ -99,8 +101,9 @@ class ArticleBuilder {
.sort((pluginA, pluginB) => (
pluginA.meta.dependency.includes(pluginB.meta.name)
? 1 : -1))
// .map(logger)
// map plugins to functions
.map(plugin => (plugin[stage]))))
.map((plugin) => (plugin[stage]))))
.reduce(async (state, plugin) => {
const resolvedState = await state;
return plugin(resolvedState);
Expand All @@ -109,9 +112,32 @@ class ArticleBuilder {
}

// (async () => {
// const builder = new ArticleBuilder();
// builder.skip.stages = [
// 'github:before',
// 'github',
// 'github:after',
// ];
// builder.skip.plugins = [
// { name: 'codepenTransform' },
// { name: 'codepenTransformIFrame' },
// { name: 'createREADME' },
// { name: 'downloadImages' },
// { name: 'writeMarkdown' },
// { name: 'TMPDir' },
// { name: 'initGithub' },
// { name: 'uploadToRepo' },
// { name: 'createRepo' },
// { name: 'createREADME' },
// { name: 'createCard' },
// ];

// try {
// const builder = new ArticleBuilder();
// await builder.create('https://www.smashingmagazine.com/2020/05/convince-others-against-dark-patterns/');
// const result = await builder.create('https://increment.com/frontend/a-users-guide-to-css-variables/');
// // console.log(result);
// console.log(result.tags);
// console.log(result.mercury[0].author);
// console.log(result.openGraph);
// } catch (error) {
// console.log(error);
// }
Expand Down
4 changes: 2 additions & 2 deletions source/libs/PluginBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
* @throw {Error} - if some dependencies not met
*/
dependencyCheck: (stack = [], dependency = [], name = null) => {
const error = dependency.find(plugin => (!stack.includes(plugin)));
const error = dependency.find((plugin) => (!stack.includes(plugin)));
if (error !== undefined) throw new Error(`Dependencies ${name ? `of ${name}` : ''} not met: ${error}`);
},

Expand All @@ -36,6 +36,6 @@ module.exports = {
*/
domainCheck: (url, domain) => {
const currentDomain = /https?:\/\/(?<domain>[^/\\]+)/ig.exec(url);
return currentDomain && (domain === currentDomain[1]);
return (domain === null) || (currentDomain && (currentDomain[1].includes(domain)));
},
};
6 changes: 2 additions & 4 deletions source/plugins/TMPDir/TMPDir.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ module.exports = deepmerge(pluginBase, {
* @return {object} - modified article state
*/
after: (unmodified) => {
return unmodified;
const {
meta: {
name,
Expand All @@ -52,7 +51,7 @@ module.exports = deepmerge(pluginBase, {
stack: [],
...unmodified,
};
if (domainCheck(url, domain)) return unmodified;
if (!domainCheck(url, domain)) return unmodified;
dependencyCheck(stack, [...dependency, `${name}:before`]);
const articleDIR = path.resolve(TMP_DIR_NAME, slug);
rimraf.sync(articleDIR);
Expand Down Expand Up @@ -88,8 +87,7 @@ module.exports = deepmerge(pluginBase, {
stack: [],
...unmodified,
};

if (domainCheck(url, domain)) return unmodified;
if (!domainCheck(url, domain)) return unmodified;
dependencyCheck(stack, dependency, name);

const articleDIR = path.resolve(TMP_DIR_NAME, slug);
Expand Down
64 changes: 64 additions & 0 deletions source/plugins/alistapart.com/alistapart.com.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const deepmerge = require('deepmerge');
const pluginBase = require('../../libs/PluginBase');
const TagExtractor = require('../../libs/TagExtractor');

/**
* @typedef {object} PluginMeta
* @property {string} name - plugin name
* @property {string[]} dependency - array of plugins that we need to run first
* @property {boolean} async - function return Promise?
*/

/**
* @namespace
* @typedef {object} Plugin
* @property {PluginMeta} meta - plugins mata data
* @property {function} before - plugin function
*/
module.exports = deepmerge(pluginBase, {
meta: {
name: 'alistapart.com',
dependency: ['createMarkdown', 'domain', 'getTags'],
domain: 'alistapart.com',
},

/**
* create README.md file
* @param {object} unmodified - current article sate
* @return {object} - modified article state
*/
[['mutation:after']]: async (unmodified) => {
const {
meta: {
name,
dependency,
domain,
},
dependencyCheck,
domainCheck,
} = module.exports;
const {
url,
stack,
domain: domainName,
dom: { original },
mercury: [page],
} = unmodified;
const modified = {
tags: [],
stack: [],
...unmodified,
};
const {
tags,
} = modified;

if (!domainCheck(url, domain)) return unmodified;
dependencyCheck(stack, dependency, name);
const extractedTags = [...original.window.document.querySelectorAll('.cat-links a')].map((element) => element.innerHTML);
modified.tags = [...extractedTags, domainName];
page.author = original.window.document.querySelector('.author.vcard[itemprop="author"] [itemprop="name"]').innerHTML;
modified.stack.push(name);
return modified;
},
});
2 changes: 1 addition & 1 deletion source/plugins/cleanDisqus/cleanDisqus.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module.exports = deepmerge(pluginBase, {
mercury,
},
} = modified;
if (domainCheck(url, domain)) return unmodified;
if (!domainCheck(url, domain)) return unmodified;
dependencyCheck(stack, dependency, name);

Array.from(mercury.window.document.querySelectorAll('[class*="disqus"],[class*="dsq-"],[id*="disqus"]'))
Expand Down
Loading

0 comments on commit 0c80455

Please sign in to comment.