Skip to content

Commit 0c80455

Browse files
committed
social microformats updated
1 parent 0e311bd commit 0c80455

File tree

31 files changed

+686
-71
lines changed

31 files changed

+686
-71
lines changed

package-lock.json

Lines changed: 112 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
{
22
"name": "@frontender-magazine/builder",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"description": "build an article from url",
5-
"main": "index.js",
5+
"main": "source/index.js",
66
"scripts": {
77
"precommit": "lint-staged",
8-
"lint": "eslint --color -f stylish --fix ./source/linttest/*.jsx"
8+
"lint": "eslint --color -f stylish --fix ./source/linttest/*.jsx",
9+
"postversion": "git push && git push --tags",
10+
"major": "npm version major && npm publish --tag latest --access public",
11+
"minor": "npm version minor && npm publish --tag latest --access public",
12+
"patch": "npm version patch && npm publish --tag latest --access public",
13+
"dopreminor": "npm version preminor && npm publish --tag next --access public",
14+
"dopremajor": "npm version premajor && npm publish --tag next --access public",
15+
"doprepatch": "npm version prepatch && npm publish --tag next --access public"
916
},
1017
"repository": {
1118
"type": "git",
@@ -73,6 +80,7 @@
7380
"joi": "^14.3.1",
7481
"jsdom": "^16.2.2",
7582
"keyword-extractor": "0.0.19",
83+
"open-graph-scraper": "^3.6.2",
7684
"path": "^0.12.7",
7785
"prettier": "^2.0.5",
7886
"pretty": "^2.0.0",

source/index.js

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ const path = require('path');
33
const fs = require('fs');
44
const { flatten } = require('array-flatten');
55

6-
// console.log(flatten);
7-
8-
// process.exit(0);
9-
106
dotenv.config();
117

8+
// eslint-disable-next-line no-unused-vars
9+
const logger = (name) => {
10+
console.log(name);
11+
return name;
12+
};
13+
1214
/**
1315
* ArticleBuilder
1416
* @class
@@ -66,20 +68,20 @@ class ArticleBuilder {
6668
};
6769
let plugins = this.pluginCollector(path.resolve('./source/plugins'));
6870
// eslint-disable-next-line import/no-dynamic-require, global-require
69-
plugins = plugins.map(uri => (require(uri)));
71+
plugins = plugins.map((uri) => (require(uri)));
7072

71-
await flatten(this.stages
73+
return flatten(this.stages
7274
// remove stages we should skip
73-
.filter(stage => (!this.skip.stages.includes(stage)))
75+
.filter((stage) => (!this.skip.stages.includes(stage)))
7476
// map stages to plugins array
75-
.map(stage => plugins
77+
.map((stage) => plugins
7678
// filter plugin that have no functions for this stage
77-
.filter(plugin => ((plugin[stage] !== undefined) && (typeof plugin[stage] === 'function')))
79+
.filter((plugin) => ((plugin[stage] !== undefined) && (typeof plugin[stage] === 'function')))
7880
// filter plugin we need to skip
7981
.filter((plugin) => {
8082
const { meta: { name } } = plugin;
8183
return (this.skip.plugins.find(
82-
skippedPlugin => (
84+
(skippedPlugin) => (
8385
(
8486
skippedPlugin.name === name
8587
&& skippedPlugin.stages === undefined
@@ -99,8 +101,9 @@ class ArticleBuilder {
99101
.sort((pluginA, pluginB) => (
100102
pluginA.meta.dependency.includes(pluginB.meta.name)
101103
? 1 : -1))
104+
// .map(logger)
102105
// map plugins to functions
103-
.map(plugin => (plugin[stage]))))
106+
.map((plugin) => (plugin[stage]))))
104107
.reduce(async (state, plugin) => {
105108
const resolvedState = await state;
106109
return plugin(resolvedState);
@@ -109,9 +112,32 @@ class ArticleBuilder {
109112
}
110113

111114
// (async () => {
115+
// const builder = new ArticleBuilder();
116+
// builder.skip.stages = [
117+
// 'github:before',
118+
// 'github',
119+
// 'github:after',
120+
// ];
121+
// builder.skip.plugins = [
122+
// { name: 'codepenTransform' },
123+
// { name: 'codepenTransformIFrame' },
124+
// { name: 'createREADME' },
125+
// { name: 'downloadImages' },
126+
// { name: 'writeMarkdown' },
127+
// { name: 'TMPDir' },
128+
// { name: 'initGithub' },
129+
// { name: 'uploadToRepo' },
130+
// { name: 'createRepo' },
131+
// { name: 'createREADME' },
132+
// { name: 'createCard' },
133+
// ];
134+
112135
// try {
113-
// const builder = new ArticleBuilder();
114-
// await builder.create('https://www.smashingmagazine.com/2020/05/convince-others-against-dark-patterns/');
136+
// const result = await builder.create('https://increment.com/frontend/a-users-guide-to-css-variables/');
137+
// // console.log(result);
138+
// console.log(result.tags);
139+
// console.log(result.mercury[0].author);
140+
// console.log(result.openGraph);
115141
// } catch (error) {
116142
// console.log(error);
117143
// }

source/libs/PluginBase.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = {
2525
* @throw {Error} - if some dependencies not met
2626
*/
2727
dependencyCheck: (stack = [], dependency = [], name = null) => {
28-
const error = dependency.find(plugin => (!stack.includes(plugin)));
28+
const error = dependency.find((plugin) => (!stack.includes(plugin)));
2929
if (error !== undefined) throw new Error(`Dependencies ${name ? `of ${name}` : ''} not met: ${error}`);
3030
},
3131

@@ -36,6 +36,6 @@ module.exports = {
3636
*/
3737
domainCheck: (url, domain) => {
3838
const currentDomain = /https?:\/\/(?<domain>[^/\\]+)/ig.exec(url);
39-
return currentDomain && (domain === currentDomain[1]);
39+
return (domain === null) || (currentDomain && (currentDomain[1].includes(domain)));
4040
},
4141
};

source/plugins/TMPDir/TMPDir.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ module.exports = deepmerge(pluginBase, {
3030
* @return {object} - modified article state
3131
*/
3232
after: (unmodified) => {
33-
return unmodified;
3433
const {
3534
meta: {
3635
name,
@@ -52,7 +51,7 @@ module.exports = deepmerge(pluginBase, {
5251
stack: [],
5352
...unmodified,
5453
};
55-
if (domainCheck(url, domain)) return unmodified;
54+
if (!domainCheck(url, domain)) return unmodified;
5655
dependencyCheck(stack, [...dependency, `${name}:before`]);
5756
const articleDIR = path.resolve(TMP_DIR_NAME, slug);
5857
rimraf.sync(articleDIR);
@@ -88,8 +87,7 @@ module.exports = deepmerge(pluginBase, {
8887
stack: [],
8988
...unmodified,
9089
};
91-
92-
if (domainCheck(url, domain)) return unmodified;
90+
if (!domainCheck(url, domain)) return unmodified;
9391
dependencyCheck(stack, dependency, name);
9492

9593
const articleDIR = path.resolve(TMP_DIR_NAME, slug);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const deepmerge = require('deepmerge');
2+
const pluginBase = require('../../libs/PluginBase');
3+
const TagExtractor = require('../../libs/TagExtractor');
4+
5+
/**
6+
* @typedef {object} PluginMeta
7+
* @property {string} name - plugin name
8+
* @property {string[]} dependency - array of plugins that we need to run first
9+
* @property {boolean} async - function return Promise?
10+
*/
11+
12+
/**
13+
* @namespace
14+
* @typedef {object} Plugin
15+
* @property {PluginMeta} meta - plugins mata data
16+
* @property {function} before - plugin function
17+
*/
18+
module.exports = deepmerge(pluginBase, {
19+
meta: {
20+
name: 'alistapart.com',
21+
dependency: ['createMarkdown', 'domain', 'getTags'],
22+
domain: 'alistapart.com',
23+
},
24+
25+
/**
26+
* create README.md file
27+
* @param {object} unmodified - current article sate
28+
* @return {object} - modified article state
29+
*/
30+
[['mutation:after']]: async (unmodified) => {
31+
const {
32+
meta: {
33+
name,
34+
dependency,
35+
domain,
36+
},
37+
dependencyCheck,
38+
domainCheck,
39+
} = module.exports;
40+
const {
41+
url,
42+
stack,
43+
domain: domainName,
44+
dom: { original },
45+
mercury: [page],
46+
} = unmodified;
47+
const modified = {
48+
tags: [],
49+
stack: [],
50+
...unmodified,
51+
};
52+
const {
53+
tags,
54+
} = modified;
55+
56+
if (!domainCheck(url, domain)) return unmodified;
57+
dependencyCheck(stack, dependency, name);
58+
const extractedTags = [...original.window.document.querySelectorAll('.cat-links a')].map((element) => element.innerHTML);
59+
modified.tags = [...extractedTags, domainName];
60+
page.author = original.window.document.querySelector('.author.vcard[itemprop="author"] [itemprop="name"]').innerHTML;
61+
modified.stack.push(name);
62+
return modified;
63+
},
64+
});

source/plugins/cleanDisqus/cleanDisqus.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module.exports = deepmerge(pluginBase, {
4949
mercury,
5050
},
5151
} = modified;
52-
if (domainCheck(url, domain)) return unmodified;
52+
if (!domainCheck(url, domain)) return unmodified;
5353
dependencyCheck(stack, dependency, name);
5454

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

0 commit comments

Comments
 (0)