Individual pages for snippet tags

Mostyl complete
This commit is contained in:
Angelos Chalaris
2018-04-07 10:43:42 +03:00
parent 02ddff180e
commit 571197d6d8
15 changed files with 2949 additions and 70 deletions

155
docs/adapter.html Normal file

File diff suppressed because one or more lines are too long

524
docs/array.html Normal file

File diff suppressed because one or more lines are too long

337
docs/browser.html Normal file

File diff suppressed because one or more lines are too long

121
docs/date.html Normal file

File diff suppressed because one or more lines are too long

275
docs/function.html Normal file

File diff suppressed because one or more lines are too long

278
docs/math.html Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

157
docs/node.html Normal file

File diff suppressed because one or more lines are too long

335
docs/object.html Normal file

File diff suppressed because one or more lines are too long

264
docs/string.html Normal file

File diff suppressed because one or more lines are too long

184
docs/type.html Normal file

File diff suppressed because one or more lines are too long

275
docs/utility.html Normal file

File diff suppressed because one or more lines are too long

View File

@ -54,6 +54,7 @@ let snippets = {},
startPart = '',
endPart = '',
output = '',
pagesOutput = [];
tagDbData = {};
// Start the timer of the script
console.time('Webber');
@ -61,8 +62,8 @@ console.time('Webber');
snippets = util.readSnippets(snippetsPath);
// Load static parts for the index.html file
try {
startPart = fs.readFileSync(path.join(staticPartsPath, 'index-start.html'), 'utf8');
endPart = fs.readFileSync(path.join(staticPartsPath, 'index-end.html'), 'utf8');
startPart = fs.readFileSync(path.join(staticPartsPath, 'page-start.html'), 'utf8');
endPart = fs.readFileSync(path.join(staticPartsPath, 'page-end.html'), 'utf8');
} catch (err) {
// Handle errors (hopefully not!)
console.log(`${chalk.red('ERROR!')} During static part loading: ${err}`);
@ -78,8 +79,7 @@ try {
for (let tag of [...new Set(Object.entries(tagDbData).map(t => t[1][0]))]
.filter(v => v)
.sort((a, b) => util.capitalize(a, true) === 'Uncategorized' ? 1 : util.capitalize(b, true) === 'Uncategorized' ? -1 : a.localeCompare(b))) {
output +=
`<h3>` +
output += `<h3>` +
md
.render(`${util.capitalize(tag, true)}\n`)
.replace(/<p>/g, '')
@ -87,7 +87,7 @@ try {
`</h3>`;
for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1][0] === tag))
output += md
.render(`[${taggedSnippet[0]}](#${taggedSnippet[0].toLowerCase()})\n`)
.render(`[${taggedSnippet[0]}](./${tag}.html#${taggedSnippet[0].toLowerCase()})\n`)
.replace(/<p>/g, '')
.replace(/<\/p>/g, '')
.replace(/<a/g, `<a class="sublink-1" tags="${taggedSnippet[1].join(',')}"`);
@ -99,11 +99,12 @@ try {
for (let tag of [...new Set(Object.entries(tagDbData).map(t => t[1][0]))]
.filter(v => v)
.sort((a, b) => util.capitalize(a, true) === 'Uncategorized' ? 1 : util.capitalize(b, true) === 'Uncategorized' ? -1 : a.localeCompare(b))) {
output += md
let localOutput = output.replace(/\$tag/g, util.capitalize(tag)).replace(new RegExp(`./${tag}.html#`,'g'),'#');
localOutput += md
.render(`## ${util.capitalize(tag, true)}\n`)
.replace(/<h2>/g, '<h2 style="text-align:center;">');
for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1][0] === tag))
output +=
localOutput +=
'<div class="card fluid">' +
md
.render(`\n${snippets[taggedSnippet[0] + '.md']}`)
@ -114,66 +115,43 @@ try {
.replace(/<\/pre>\s+<pre/g, '</pre><label class="collapse">Show examples</label><pre') +
'<button class="primary clipboard-copy">&#128203;&nbsp;Copy to clipboard</button>' +
'</div></div>';
// Add the ending static part
localOutput += `\n${endPart + '\n'}`;
localOutput = util.optimizeNodes(localOutput, /<span class="token punctuation">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token punctuation">([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `<span class="token punctuation">${p1}${p2}${p3}</span>`);
// Optimize operator nodes
localOutput = util.optimizeNodes(localOutput, /<span class="token operator">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token operator">([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `<span class="token operator">${p1}${p2}${p3}</span>`);
// Optimize keyword nodes
localOutput = util.optimizeNodes(localOutput, /<span class="token keyword">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token keyword">([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `<span class="token keyword">${p1}${p2}${p3}</span>`);
pagesOutput.push({'tag': tag,'content': localOutput});
}
// Add the ending static part
output += `\n${endPart + '\n'}`;
// Optimize punctuation nodes
output = util.optimizeNodes(output, /<span class="token punctuation">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token punctuation">([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `<span class="token punctuation">${p1}${p2}${p3}</span>`);
// Optimize operator nodes
output = util.optimizeNodes(output, /<span class="token operator">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token operator">([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `<span class="token operator">${p1}${p2}${p3}</span>`);
// Optimize keyword nodes
output = util.optimizeNodes(output, /<span class="token keyword">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token keyword">([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `<span class="token keyword">${p1}${p2}${p3}</span>`);
// do {
// const punctuationRegex = /<span class="token punctuation">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token punctuation">([^\0]*?)<\/span>/gm;
// output = output.replace(punctuationRegex,
// (match, p1, p2, p3) => `<span class="token punctuation">${p1}${p2}${p3}</span>`
// );
// count = 0;
// while (punctuationRegex.exec(output) !== null) {
// ++count;
// }
// } while (count > 0);
// // Optimize punctuation nodes
// output = util.optimizeNodes(output, /<span class="token punctuation">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token punctuation">([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `<span class="token punctuation">${p1}${p2}${p3}</span>`);
// // Optimize operator nodes
// do {
// const operatorRegex = /<span class="token operator">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token operator">([^\0]*?)<\/span>/gm;
// output = output.replace(operatorRegex,
// (match, p1, p2, p3) => `<span class="token operator">${p1}${p2}${p3}</span>`
// );
// count = 0;
// while (operatorRegex.exec(output) !== null) {
// ++count;
// }
// } while (count > 0);
// output = util.optimizeNodes(output, /<span class="token operator">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token operator">([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `<span class="token operator">${p1}${p2}${p3}</span>`);
// // Optimize keyword nodes
// do {
// const keyWordRegex = /<span class="token keyword">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token keyword">([^\0]*?)<\/span>/gm;
// output = output.replace(keyWordRegex,
// (match, p1, p2, p3) => `<span class="token keyword">${p1}${p2}${p3}</span>`
// );
// count = 0;
// while (keyWordRegex.exec(output) !== null) {
// ++count;
// }
// } while (count > 0);
// output = util.optimizeNodes(output, /<span class="token keyword">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token keyword">([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `<span class="token keyword">${p1}${p2}${p3}</span>`);
// Minify output
output = minify(output, {
collapseBooleanAttributes: true,
collapseWhitespace: true,
decodeEntities: false,
minifyCSS: true,
minifyJS: true,
keepClosingSlash: true,
processConditionalComments: true,
removeAttributeQuotes: false,
removeComments: true,
removeEmptyAttributes: false,
removeOptionalTags: false,
removeScriptTypeAttributes: false,
removeStyleLinkTypeAttributes: false,
trimCustomFragments: true
});
pagesOutput.forEach(page => {
page.content = minify(page.content, {
collapseBooleanAttributes: true,
collapseWhitespace: true,
decodeEntities: false,
minifyCSS: true,
minifyJS: true,
keepClosingSlash: true,
processConditionalComments: true,
removeAttributeQuotes: false,
removeComments: true,
removeEmptyAttributes: false,
removeOptionalTags: false,
removeScriptTypeAttributes: false,
removeStyleLinkTypeAttributes: false,
trimCustomFragments: true
});
fs.writeFileSync(path.join(docsPath, page.tag+'.html'), page.content);
})
// Write to the index.html file
fs.writeFileSync(path.join(docsPath, 'index.html'), output);
//fs.writeFileSync(path.join(docsPath, 'index.html'), output);
} catch (err) {
// Handle errors (hopefully not!)
console.log(`${chalk.red('ERROR!')} During index.html generation: ${err}`);

View File

@ -2,7 +2,7 @@
<html lang="en">
<head>
<link rel="stylesheet" href="./mini.css">
<title>30 seconds of code</title>
<title>$tag - 30 seconds of code</title>
<meta charset="utf-8">
<meta name="description" content="Curated collection of useful Javascript snippets that you can understand in 30 seconds or less.">
<meta name="keywords" content="javascript, snippets, code, programming">
@ -53,6 +53,7 @@
window.setTimeout(function(){
document.getElementById('disclaimer').style.display = 'none';
}, 30000);
document.querySelector('nav').scrollTop = Array.from(document.querySelectorAll('h3')).filter(v => v.innerHTML === '$tag')[0].offsetTop - 60;
}
function registerClickListener() {
document.addEventListener('click', function (event) {
@ -87,6 +88,7 @@
scrollToTop();
}
else if (event.target.classList.contains('sublink-1')){
if(event.target.getAttribute("href").includes('./')) return;
event.preventDefault();
scrollTo(document.querySelector('main'), document.getElementById(event.target.href.split('#')[1]).parentElement.offsetTop - 60, event.target.href.split('#')[1], 400);
document.getElementById('doc-drawer-checkbox').checked = false;
@ -96,12 +98,6 @@
</script>
</head>
<body onload="loader()">
<div class="card" id="disclaimer">
<div class="section double-padded">
<p><strong>⚠️ WARNING:</strong> Snippets are not production ready.</p>
<button id="disclaimer-close" onclick="this.parentElement.style.display = 'none';"></button>
</div>
</div>
<a href="https://github.com/Chalarangelo/30-seconds-of-code" class="github-corner" aria-label="View source on Github"><svg width="56" height="56" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0; z-index: 1000" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
<header style="height: 3.5rem; box-sizing: border-box; overflow: hidden;">
<h1 class="logo"><img src="favicon.png" style="height: 3.5rem;" alt="logo"/><span id="title">&nbsp;30 seconds of code</span>