Skip to content

Commit

Permalink
set up prettier cli
Browse files Browse the repository at this point in the history
  • Loading branch information
fejes713 committed Feb 24, 2019
1 parent 9769799 commit 04fa551
Show file tree
Hide file tree
Showing 15 changed files with 4,126 additions and 290 deletions.
3,883 changes: 3,852 additions & 31 deletions docs/website.f992e568.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"builder": "node ./src/builder.js",
"dev": "parcel website/index.html",
"build": "parcel build website/index.html -d docs/ --public-url ./",
"format": "prettier --write '**/*.js'",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Stefan Fejes <[email protected]> (https://stefanfejes.com)",
Expand All @@ -40,6 +41,7 @@
"fs-extra": "^7.0.1",
"markdown-builder": "^0.9.0",
"node-sass": "^4.11.0",
"parcel-bundler": "^1.11.0"
"parcel-bundler": "^1.11.0",
"prettier": "^1.16.4"
}
}
61 changes: 31 additions & 30 deletions src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ const markdown = require("markdown-builder");
const snippets = require("../data.json");

const { headers, misc, lists } = markdown;
const CATEGORY_NAMES = [
'Tips',
'Pro Tips',
'Facts'
];
const CATEGORY_NAMES = ["Tips", "Pro Tips", "Facts"];

const STATIC_PARTS_PATH = "./static-parts";

Expand All @@ -19,23 +15,27 @@ let endPart = "";
let output = "";

const detailsTOC = (title, snippetsArray) =>
`\n${misc.collapsible(
title,
lists.ul(snippetsArray, snippet =>
misc.link(
snippet.shortTip
.replace("\n", "")
.split("```")[0]
.trim(),
misc.anchor(
snippet.shortTip
.replace("\n", "")
.split("```")[0]
.trim()
`\n${misc
.collapsible(
title,
lists
.ul(snippetsArray, snippet =>
misc.link(
snippet.shortTip
.replace("\n", "")
.split("```")[0]
.trim(),
misc.anchor(
snippet.shortTip
.replace("\n", "")
.split("```")[0]
.trim()
)
)
)
)
).trim()
).trim()}\n\n`;
.trim()
)
.trim()}\n\n`;

console.time("Builder");

Expand All @@ -59,10 +59,12 @@ try {

const snippetsInTag = {};

CATEGORY_NAMES.forEach((tag, i) => snippetsInTag[i] = snippets.filter(v => v.category == i));
CATEGORY_NAMES.forEach(
(tag, i) => (snippetsInTag[i] = snippets.filter(v => v.category == i))
);

// write Table of Contents
CATEGORY_NAMES.forEach((tag,i) => {
CATEGORY_NAMES.forEach((tag, i) => {
const taggedSnippets = snippetsInTag[i];
output += headers.h3(util.capitalize(tag));
output += detailsTOC("View contents", taggedSnippets);
Expand All @@ -72,21 +74,21 @@ try {
output += misc.hr();

// write actual snippets
CATEGORY_NAMES.forEach((tag,i) => {
CATEGORY_NAMES.forEach((tag, i) => {
output += headers.h2(util.capitalize(tag));
const taggedSnippets = snippetsInTag[i];
taggedSnippets.forEach(snippet => {
output += headers.h3(snippet.shortTip).trim();
output += `\n\n${snippet.longTip}`;
if (snippet.links && snippet.links.length) {
output += headers.h4('Resources');
output += `\n${snippet.links.map(v => `* ${v}`).join('\n')}`;
output += headers.h4("Resources");
output += `\n${snippet.links.map(v => `* ${v}`).join("\n")}`;
}
output += `\n<br>${misc.link(
"⬆ Back to top",
misc.anchor("Table of Contents")
)}\n\n`
})
)}\n\n`;
});
});

// add static part for end
Expand All @@ -98,6 +100,5 @@ try {
process.exit(1);
}


console.log(`${chalk.green("SUCCESS!")} README file generated!`);
console.timeEnd("Builder");
console.timeEnd("Builder");
77 changes: 39 additions & 38 deletions src/extract.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
// This script converts markdown files into JSON format
const fs = require("fs-extra")
const path = require("path")
const chalk = require("chalk")
const fs = require("fs-extra");
const path = require("path");
const chalk = require("chalk");
const {
attempt,
readData,
getSection,
getShortTip,
getLongTip,
CATEGORY_NAMES
} = require("./util")
attempt,
readData,
getSection,
getShortTip,
getLongTip,
CATEGORY_NAMES
} = require("./util");

console.time("Extractor")
console.time("Extractor");

attempt("data.json generation", () => {
const output = Object.entries(readData()).map(([name, contents]) => {
const shortTip = getShortTip(contents)
const longTip = getLongTip(contents)
const output = Object.entries(readData()).map(([name, contents]) => {
const shortTip = getShortTip(contents);
const longTip = getLongTip(contents);

const links = getSection("### Resources", contents)
.split("\n")
.filter(v =>
/(\/\*[\w\'\s\r\n\*]*\*\/)|(\/\/[\w\s\']*)|(\<![\-\-\s\w\>\/]*\>)/.test(
v
)
)
.map(v => v.replace(/[*-] /g, ""))
.filter(v => v.trim() !== "" && !v.includes("category"))
const links = getSection("### Resources", contents)
.split("\n")
.filter(v =>
/(\/\*[\w\'\s\r\n\*]*\*\/)|(\/\/[\w\s\']*)|(\<![\-\-\s\w\>\/]*\>)/.test(
v
)
)
.map(v => v.replace(/[*-] /g, ""))
.filter(v => v.trim() !== "" && !v.includes("category"));

const categoryNum = parseInt(
(contents.match(/<!--\s*category:\s*\((.+)\)/) || [])[1],
10)
return {
name,
shortTip,
longTip,
links,
category: CATEGORY_NAMES[categoryNum]
}
})
const categoryNum = parseInt(
(contents.match(/<!--\s*category:\s*\((.+)\)/) || [])[1],
10
);
return {
name,
shortTip,
longTip,
links,
category: CATEGORY_NAMES[categoryNum]
};
});

fs.writeFileSync("./data.json", JSON.stringify(output, null, 2))
})
fs.writeFileSync("./data.json", JSON.stringify(output, null, 2));
});

console.log(`${chalk.green("SUCCESS!")} data.json file generated!`)
console.timeEnd("Extractor")
console.log(`${chalk.green("SUCCESS!")} data.json file generated!`);
console.timeEnd("Extractor");
123 changes: 61 additions & 62 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,85 @@
const fs = require("fs-extra")
const path = require("path")
const chalk = require("chalk")
const fs = require("fs-extra");
const path = require("path");
const chalk = require("chalk");

const DATA_PATH = "./data"
const DATA_PATH = "./data";

const CATEGORY_NAMES = {
0: "tip",
1: "pro tip",
2: "you should know"
}
0: "tip",
1: "pro tip",
2: "you should know"
};

const attempt = (task, cb) => {
try {
return cb()
} catch (e) {
console.log(`${chalk.red("ERROR!")} During ${task}: ${e}`)
process.exit(1)
return null
}
}
try {
return cb();
} catch (e) {
console.log(`${chalk.red("ERROR!")} During ${task}: ${e}`);
process.exit(1);
return null;
}
};

const readData = () =>
attempt("read accessibility tips", () =>
fs
.readdirSync(DATA_PATH)
.filter(f => !f.includes("eslint"))
.sort((a, b) => (a.toLowerCase() < b.toLowerCase() ? -1 : 1))
.reduce((acc, name) => {
acc[name] = fs
.readFileSync(path.join(DATA_PATH, name), "utf8")
.replace(/\r\n/g, "\n")
return acc
}, {})
)
attempt("read accessibility tips", () =>
fs
.readdirSync(DATA_PATH)
.filter(f => !f.includes("eslint"))
.sort((a, b) => (a.toLowerCase() < b.toLowerCase() ? -1 : 1))
.reduce((acc, name) => {
acc[name] = fs
.readFileSync(path.join(DATA_PATH, name), "utf8")
.replace(/\r\n/g, "\n");
return acc;
}, {})
);

const capitalize = ([first, ...rest], lowerRest = false) =>
first.toUpperCase() +
(lowerRest ? rest.join("").toLowerCase() : rest.join(""))
first.toUpperCase() +
(lowerRest ? rest.join("").toLowerCase() : rest.join(""));

const getSection = (searchString, contents, includeSubsections = true) => {
const indexOfSearch = contents.indexOf(searchString)
if (indexOfSearch < 0) return ""
const indexOfSearch = contents.indexOf(searchString);
if (indexOfSearch < 0) return "";

let endSearch = "\\n#"
if (includeSubsections) {
let i
for (i = 0; searchString[i] === "#" && i < searchString.length; i++);
let endSearch = "\\n#";
if (includeSubsections) {
let i;
for (i = 0; searchString[i] === "#" && i < searchString.length; i++);

if (i > 0) {
endSearch += `{${i - 1},${i}}[^#]`
}
if (i > 0) {
endSearch += `{${i - 1},${i}}[^#]`;
}
const endRegex = new RegExp(endSearch)
}
const endRegex = new RegExp(endSearch);

const sliceStart = indexOfSearch + searchString.length + 1
const endIndex = contents.slice(sliceStart).search(endRegex)
const sliceEnd = endIndex === -1 ? undefined : endIndex + sliceStart
const sliceStart = indexOfSearch + searchString.length + 1;
const endIndex = contents.slice(sliceStart).search(endRegex);
const sliceEnd = endIndex === -1 ? undefined : endIndex + sliceStart;

return contents.slice(sliceStart, sliceEnd).trim()
}
return contents.slice(sliceStart, sliceEnd).trim();
};

const getFirstSection = (contents, includeSubsections) =>
getSection("", contents.slice(3), includeSubsections)
getSection("", contents.slice(3), includeSubsections);

const getLongTip = str => {
const regex = /^.*\n*([\s\S]*?)(###)|(<!--)/;
const results = [];
let m = regex.exec(str);
return m[1];
const regex = /^.*\n*([\s\S]*?)(###)|(<!--)/;
const results = [];
let m = regex.exec(str);
return m[1];
};

const getShortTip = (contents) =>
contents.split('\n')[0].replace(/^#+\s+/g, '');
const getShortTip = contents => contents.split("\n")[0].replace(/^#+\s+/g, "");

module.exports = {
attempt,
readData,
capitalize,
DATA_PATH,
CATEGORY_NAMES,
getSection,
getFirstSection,
getLongTip,
getShortTip
}
attempt,
readData,
capitalize,
DATA_PATH,
CATEGORY_NAMES,
getSection,
getFirstSection,
getLongTip,
getShortTip
};
14 changes: 7 additions & 7 deletions website/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { app } from "hyperapp"
import "./css/index.scss"
import "./js/browser"
import { app } from "hyperapp";
import "./css/index.scss";
import "./js/browser";

import state from "./js/state"
import actions from "./js/actions"
import view from "./js/view"
import state from "./js/state";
import actions from "./js/actions";
import view from "./js/view";

app(state, actions, view, document.body)
app(state, actions, view, document.body);
Loading

0 comments on commit 04fa551

Please sign in to comment.