NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

//Node-----------------------------------------
var fs = require('fs');
var path = require('path');
var del = require('del');
//Gulp-----------------------------------------
var gulp = require('gulp');
//General Use----------------------------------
var rename = require('gulp-rename');
var watch = require('gulp-watch');
//var gulpif = require('gulp-if'); //Is this required anymore?
var tap = require('gulp-tap');
//var merge = require('merge-stream');
//Templating-----------------------------------
var swig = require('gulp-swig');
var fm = require('front-matter');
var frontMatter = require('gulp-front-matter');
var minifyHTML = require('gulp-minify-html');
//Style/Script---------------------------------
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var minifyCSS = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
//Server---------------------------------------
var browserSync = require('browser-sync');


var addSourcemaps = true;





var prefixSettings = {
browsers: ['> 0%', 'ie >= 7'],
remove: false //Remove existing prefixes
};
var minifyHTMLOptions = {
conditionals: true,
spare: true,
empty: true
};




var path = {
src: {
styles: 'assets/styles',
scripts: 'assets/scripts',
templates: 'templates',
files: 'assest/files',
globalJSON: 'settings/global.json',
navJSONAppend: 'common.js',
navJSON: 'settings/nav.json',
navDebugJSON: 'settings/debug/nav.debug.json'
},
dist: {
styles: 'build/assets/styles',
scripts: 'build/assets/scripts',
build: 'build'
}
};

//Server settings (Browsersync)
//http://www.browsersync.io/docs/options/
var serverSettings = {
host: 'localhost',
port: 8888,
ui: {
port: 9999,
weinre: {
port: 8899
}
},
server: {
baseDir: path.dist.build
},
ghostMode: true,
online: false,
watchOptions: {
debounceDelay: 500
}
};

var pathToRoot = path.join(__dirname, path.src.templates);

//Relative/Absolute file paths
var relativeRoot="", pathFromRoot="";
// //All navigation JSON
var navigation = updateNav();
// //Create falttened navigation JSON
var flatNavigation = flattenNav(navigation);
var assetPath = {
images: 'assets/images/',
styles: 'assets/styles/',
scripts: 'assets/scripts/',
files: 'assets/files/'
};
var filesizeSettings = {
KBDecimals: 0,
KBPostOutput: " KB",
MBDecimals: 1,
MBPostOutput: " MB",
ExtUpCase: true
};


var allLocalJSON = dirTree(src.path.templates);


gulp.task('clean', function (cb) {
del(path.dist.build, cb);
});
gulp.task('server', function() {
browserSync(serverSettings);
});
gulp.task('sass', function() {
return gulp.src(path.src.styles+'/**/*.scss', {sourcemaps: addSourcemaps})
.pipe(sass())
.pipe(rename({suffix: '.min'}))
.pipe(minifyCSS())
.pipe(autoprefixer(prefixSettings))
.pipe(gulp.dest(path.dist.build));
});
gulp.task('css', function() {
return gulp.src(path.src.styles+'/**/*.css', {sourcemaps: addSourcemaps})
.pipe(rename({suffix: '.min'}))
.pipe(minifyCSS())
.pipe(autoprefixer(prefixSettings))
.pipe(gulp.dest(path.dist.build));
});
gulp.task('css:min', function() {
return gulp.src(path.src.styles+'/**/*.min.css')
.pipe(gulp.dest(path.dist.build));
});
gulp.task('scripts', function() {
return gulp.src(path.src.scripts+'/**/*.js', {sourcemaps: addSourcemaps})
.pipe(rename({suffix: '.min'}))
.pipe(tap(function(file) {
//If correct script file to append JSON, find "var navJSON={};" and replace with navigation
if(path.src.navJSONAppend == path.basename(file.path)) {
var output = file.contents.toString().replace(/var navJSON={};/, 'var navJSON='+JSON.stringify(navigation)+';');
file.contents = new Buffer(output);
}
}))
.pipe(uglify())
.pipe(gulp.dest(path.dist.scripts));
});
gulp.task('scripts:min', function() {
return gulp.src(path.src.scripts+'/**/*.min.js')
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest(path.dist.scripts));
});
gulp.task('swig:all', function() {
return gulp.src([path.src.templates+'/**/*.swig', '!'+path.src.templates+'/{includes,layouts}/*'])
.pipe(tap(pathTap))
.pipe(frontMatter({property: 'data'}))
.pipe(swig({setup: swigSetup, defaults: {cache: false, locals: {global: getGlobalJSON(), nav: navigation}}}))
.pipe(minifyHTML(minifyHTMLOptions))
.pipe(gulp.dest(path.dist.build));
});
gulp.task('swig:single', function() {
return gulp.src([path.src.templates+'/**/*.swig', '!'+path.src.templates+'/{includes,layouts}/*'], { since: gulp.lastRun('swig:single') })
.pipe(tap(pathTap))
.pipe(frontMatter({property: 'data'}))
.pipe(swig({setup: swigSetup, defaults: {cache: false, locals: {global: getGlobalJSON(), nav: navigation}}}))
.pipe(minifyHTML(minifyHTMLOptions))
.pipe(gulp.dest(path.dist.build));
});
function swigSingle() {
var originalNav = allLocalJSON;
allLocalJSON = dirTree(srcPath.templates);

if(JSON.stringify(originalNav) != JSON.stringify(allLocalJSON)) {
navigation = updateNav(); //Update Navigation
flatNavigation = flattenNav(navigation);
return gulp.series('swig:all', browserSync.reload);
}
else {
return gulp.series('swig:single', browserSync.reload);
}
}





gulp.task('watch', function() {
gulp.watch(path.styles+'/**/*.scss', gulp.series('sass', browserSync.reload));
gulp.watch(path.styles+'/**/*.css', gulp.series('css', browserSync.reload));
gulp.watch(path.styles+'/**/*.min.css', gulp.series('css:min', browserSync.reload));

gulp.watch(path.scripts+'/**/*.js', gulp.series('scripts', browserSync.reload));
gulp.watch(path.src.navJSON, gulp.series(updateNav, 'scripts', browserSync.reload));
gulp.watch(path.scripts+navJSONAppend, gulp.series(updateNav, 'scripts', browserSync.reload));
gulp.watch(path.scripts+'/**/*.min.js', gulp.series('scripts:min', browserSync.reload));

gulp.watch(path.src.templates+'/{includes,layouts}/*', gulp.series('swig:all', browserSync.reload));
gulp.watch([path.src.templates+'/**/*.swig', '!'+path.src.templates+'/{includes,layouts}/*'],
gulp.series(swigSingle, browserSync.reload));

gulp.watch(path.src.globalJSON, gulp.series('swig:all', browserSync.reload));
gulp.watch(path.src.navDebugJSON, gulp.series('swig:all', browserSync.reload));
});


gulp.task('default', gulp.series('server', 'watch'));
gulp.task('build', gulp.series('clean', gulp.parallel('sass', 'css', 'css:min', 'scripts', 'scripts:min', 'swig:all')));







//Functions
function pathTap(file) {
relativeRoot = path.relative(path.dirname(file.path), pathToRoot).replace(/\/g,"/");
pathFromRoot = file.path.replace(pathToRoot, "").replace(/\/g,"/").replace(/.swig/, ".html");
}
function getGlobalJSON() {
return JSON.parse(fs.readFileSync('./'+path.src.globalJSON, 'utf8'));
}
function updateNav() {
console.log("NOTICE| Updating navigation");
var nav = JSON.parse(fs.readFileSync(path.src.navJSON, 'utf8')),
pages = dirTree(path.src.templates);

var copyNodes = function(source, collection) {
for(var c in collection) {
var item = collection[c];

if(item["id"] == source["id"]) {
delete source["id"];
for(var prop in source) {
if("url" in item && prop == "url") { continue; }

item[prop] = source[prop];
}
} else if(item["children"] !== "undefined" && typeof(item["children"] == "object")) {
copyNodes(source, item["children"]);
}
}
return;
};

for(var p in pages) {
var page = pages[p];
copyNodes(page, nav.nav);
}

//Save generated navigation file for debug.
fs.writeFileSync(path.src.navDebugJSON, JSON.stringify(nav, null, 4));

return nav;
}
function flattenNav(treeNav) {
var flatNav = [];

function recurseNav(cur, parent) {
//Copy JSON structure by value (NOT REFERENCE)
var thisObj = JSON.parse(JSON.stringify(cur));
//Add parent attr to JSON
thisObj.parent = parent;

//We want to recursively add children separately
delete thisObj["children"];

if(cur.hasOwnProperty("children")) {
thisObj.children = [];
for(var child in cur["children"]) {
thisObj.children.push(cur["children"][child]["id"]);
recurseNav(cur["children"][child], cur["id"]);
}
}
flatNav.push(thisObj);
}

recurseNav(treeNav.nav[0], "rootNav");
fs.writeFileSync(path.src.navFlatDebugJSON, JSON.stringify(flatNav, null, 4));
return flatNav;
}

function dirTree(dir) {
var stats = fs.lstatSync(dir),
data = [],
exclude = ["global.json",
"nav.json",
"includes",
"layouts"];

if(stats.isDirectory()) {
var items = fs.readdirSync(dir);

for(var i=0; i < items.length; i++ ) {
var item = items[i];

if(exclude.indexOf(path.basename(item)) === -1 && path.extname(item) == ".swig") {
if(path.extname(item) == ".swig") {
try {
var content = fm(fs.readFileSync(dir + '/' + item, 'utf8')).attributes;
if(Object.keys(content).length > 0) { //Check if YAML headers are empty
if(!content.hasOwnProperty("url")) {
content.url = dir.replace(srcPath.templates, "") + '/' + path.normalize(item).replace(/.swig/, ".html");
}
data.push(content);
}
}
catch(e) {console.log("ERROR| Parsing YAML Header: "+e);}
}
} else if(fs.lstatSync(dir + '/' + item).isDirectory()) {
data = data.concat.apply(data, dirTree(dir + '/' + item));
}
}
}
return data;
}

//Swig configuration
function swigSetup(swig) {
swig.setDefaults({
loader: swig.loaders.fs(__dirname + '/' + path.src.templates)
});
swig.setFilter('image', function(input, idx) {
//Check to see if already in root directory
var relativeImages = (relativeRoot === '') ? assetPath.images : relativeRoot +'/'+ assetPath.images;
return relativeImages + input;
});
swig.setFilter('style', function(input, idx) {
var relativeStyles = (relativeRoot === '') ? assetPath.styles : relativeRoot +'/'+ assetPath.styles;
return relativeStyles + input;
});
swig.setFilter('script', function(input, idx) {
var relativeScripts = (relativeRoot === '') ? assetPath.scripts : relativeRoot +'/'+ assetPath.scripts;
return relativeScripts + input;
});
swig.setFilter('file', function(input, idx) {
var relativeFiles = (relativeRoot === '') ? assetPath.files : relativeRoot +'/'+ assetPath.files;
return relativeFiles + input;
});
swig.setFilter('filesize', function(input, idx) {
var filePath = path.src.files+'/'+input;
var fileBytes = fs.statSync(filePath)["size"];
if(fileBytes >= 1000000) {
return (fileBytes / 1000000).toFixed(filesizeSettings.MBDecimals);
}
else {
return (fileBytes / 1000).toFixed(filesizeSettings.KBDecimals);
}
});
swig.setFilter('filesizeUnit', function(input, idx) {
var filePath = path.src.files+'/'+input;
var fileBytes = fs.statSync(filePath)["size"];
if(fileBytes >= 1000000) {
return (fileBytes / 1000000).toFixed(filesizeSettings.MBDecimals) + filesizeSettings.MBPostOutput;
}
else {
return (fileBytes / 1000).toFixed(filesizeSettings.KBDecimals) + filesizeSettings.KBPostOutput;
}
});
swig.setFilter('fileExtSize', function(input, idx) {
var filePath = path.src.files+'/'+input;
var fileExt = path.extname(input);
//Remove '.' from extension
fileExt = fileExt.replace(/./g, '');
//Uppercase or lowercase extension
fileExt = filesizeSettings.ExtUpCase ? fileExt.toUpperCase() : fileExt.toLowerCase();

var fileBytes = fs.statSync(filePath)["size"];
if(fileBytes >= 1000000) {
return "(" + fileExt + ", " + (fileBytes / 1000000).toFixed(filesizeSettings.MBDecimals) + filesizeSettings.MBPostOutput + ")";
}
else {
return "(" + fileExt + ", " + (fileBytes / 1000).toFixed(filesizeSettings.KBDecimals) + filesizeSettings.KBPostOutput + ")";
}
});
swig.setFilter('link', function(input, idx) {
var relativeLink = path.relative(path.dirname(pathFromRoot), input).replace(/\/g,"/");
return relativeLink;
});
swig.setFilter('linkid', function(input, idx) {
var thisUrl = searchJSONFlat(input, flatNavigation).url;
if(typeof thisUrl != "undefined") {
return path.relative(path.dirname(pathFromRoot), thisUrl).replace(/\/g,"/");
}
else {
console.log("WARNING| linkid filter ID: " + input + ' was not found in: ' + pathFromRoot);
return "#";
}
});
//Return object by id
swig.setFilter('obj', function(input, idx) {
var result = searchJSONFlat(input, flatNavigation);
if(typeof result != "undefined") {
return result;
}
else {
console.log("WARNING| obj filter ID: " + input + ' was not found in: ' + pathFromRoot);
return "#";
}
});
}
function searchJSONFlat(id, flatJSON) {
for(var i=0;i<flatJSON.length;i++) {
if(flatJSON[i].id == id) {
return flatJSON[i];
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.