Move all src JS into external plugins installed through NPM:

@11ty/eleventy-plugin-rss
@11ty/eleventy-plugin-syntaxhighlight
master
Zach Leatherman 2018-01-28 21:27:41 -06:00
parent c4b6550f4e
commit ac5c7edb44
11 changed files with 14 additions and 235 deletions

View File

@ -1,50 +1,17 @@
const { DateTime } = require("luxon");
const metadata = require("./_data/metadata.json");
const absoluteUrl = require("./_src/AbsoluteUrl");
const HtmlToAbsoluteUrls = require("./_src/HtmlToAbsoluteUrls");
const syntaxHighlighter = require("./_src/eleventy-liquidjs-tag-highlight-prismjs");
function dateToISO(dateObj) {
return DateTime.fromJSDate(dateObj).toISO({ includeOffset: true, suppressMilliseconds: true });
}
const pluginRss = require("@11ty/eleventy-plugin-rss");
const pluginSyntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
module.exports = function(eleventyConfig) {
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(pluginSyntaxHighlight);
eleventyConfig.addLayoutAlias("post", "layouts/post.njk");
eleventyConfig.addFilter("rssLastUpdatedDate", collection => {
if( !collection.length ) {
throw new Error( "Collection is empty in lastUpdatedDate filter." );
}
// Newest date in the collection
return dateToISO(collection[ collection.length - 1 ].date);
});
eleventyConfig.addFilter("rssDate", dateObj => {
return dateToISO(dateObj);
});
eleventyConfig.addFilter("readableDate", dateObj => {
return DateTime.fromJSDate(dateObj).toFormat("dd LLL yyyy");
});
eleventyConfig.addNunjucksFilter("absoluteUrl", function(href, base) {
return absoluteUrl(href, base);
});
eleventyConfig.addNunjucksFilter("htmlToAbsoluteUrls", function(htmlContent, base, callback) {
if(!htmlContent) {
callback(null, "");
return;
}
HtmlToAbsoluteUrls(htmlContent, base).then(result => {
callback(null, result.html);
});
}, true);
// compatibility with existing {% highlight js %} and others
eleventyConfig.addLiquidTag("highlight", syntaxHighlighter);
// only content in the `posts/` directory
eleventyConfig.addCollection("posts", function(collection) {
return collection.getAllSorted().filter(function(item) {

View File

@ -1,5 +0,0 @@
const { URL } = require("url");
module.exports = function(url, base) {
return (new URL(url, base)).toString()
};

View File

@ -1,33 +0,0 @@
class HighlightLines {
constructor(rangeStr) {
this.highlights = this.convertRangeToHash(rangeStr);
}
convertRangeToHash(rangeStr) {
let hash = {};
if( !rangeStr ) {
return hash;
}
let ranges = rangeStr.split(",").map(function(range) {
return range.trim();
});
for(let range of ranges) {
let startFinish = range.split('-');
let start = parseInt(startFinish[0], 10);
let end = parseInt(startFinish[1] || start, 10);
for( let j = start, k = end; j<=k; j++ ) {
hash[j] = true;
}
}
return hash;
}
isHighlighted(lineNumber) {
return !!this.highlights[lineNumber]
}
}
module.exports = HighlightLines;

View File

@ -1,22 +0,0 @@
const posthtml = require('posthtml');
const urls = require('posthtml-urls')
const absoluteUrl = require("./AbsoluteUrl");
module.exports = function(htmlContent, base) {
let options = {
eachURL: function(url, attr, element) {
url = url.trim();
// #anchor in-page
if( url.indexOf("#") === 0 ) {
return url;
}
return absoluteUrl(url, base);
}
};
let modifier = posthtml().use(urls(options));
return modifier.process(htmlContent);
};

View File

@ -1,83 +0,0 @@
const HighlightLines = require('./HighlightLines');
class LiquidHighlight {
constructor(liquidEngine) {
this.liquidEngine = liquidEngine;
this.hooks = [];
this.classHooks = [];
}
addHook(hookFunction) {
this.hooks.push(hookFunction);
}
addClassHook(hookFunction) {
this.classHooks.push(hookFunction);
}
getObject() {
let ret = function(highlighter) {
return {
parse: function(tagToken, remainTokens) {
let split = tagToken.args.split(" ");
this.language = split[0];
this.highlights = new HighlightLines(split.length === 2 ? split[1] : "");
this.highlightsAdd = new HighlightLines(split.length === 3 ? split[1] : "");
this.highlightsRemove = new HighlightLines(split.length === 3 ? split[2] : "");
this.tokens = [];
var stream = highlighter.liquidEngine.parser.parseStream(remainTokens);
stream
.on('token', token => {
if (token.name === 'endhighlight') {
stream.stop();
} else {
this.tokens.push(token);
}
})
.on('end', x => {
throw new Error("tag highlight not closed");
});
stream.start();
},
render: function(scope, hash) {
let tokens = this.tokens.map(token => token.raw);
let tokenStr = tokens.join('').trim();
for( let hook of highlighter.hooks ) {
tokenStr = hook.call(this, this.language, tokenStr);
}
let lines = tokenStr.split("\n").map(function(line, j) {
let classHookClasses = [];
for( let classHook of highlighter.classHooks ) {
let ret = classHook(this.language, line, j);
if( ret ) {
classHookClasses.push(ret);
}
}
return '<div class="highlight-line' +
(this.highlights.isHighlighted(j) ? ' highlight-line-active' : '') +
(this.highlightsAdd.isHighlighted(j) ? ' highlight-line-add' : '') +
(this.highlightsRemove.isHighlighted(j) ? ' highlight-line-remove' : '') +
(classHookClasses.length ? " " + classHookClasses.join(" ") : "") +
'">' +
line +
'</div>';
}.bind(this));
return Promise.resolve(`<pre class="language-${this.language}"><code class="language-${this.language}">` + lines.join("") + "</code></pre>");
}
};
};
return ret(this);
}
}
module.exports = LiquidHighlight;

View File

@ -1,16 +0,0 @@
const LiquidHighlight = require( "./LiquidHighlight" );
module.exports = function(liquidEngine) {
let highlight = new LiquidHighlight(liquidEngine);
highlight.addClassHook(function(language, line) {
if( language === "dir" ) {
// has trailing slash
if( line.match(/\/$/) !== null ) {
return "highlight-line-isdir";
}
}
});
return highlight.getObject();
};

View File

@ -1,12 +0,0 @@
const Prism = require('prismjs');
const LiquidHighlight = require( "./LiquidHighlight" );
module.exports = function(liquidEngine) {
let highlight = new LiquidHighlight(liquidEngine);
highlight.addHook(function(language, htmlStr, lines) {
return Prism.highlight(htmlStr, Prism.languages[ language ]);
});
return highlight.getObject();
};

View File

@ -1,9 +0,0 @@
import test from "ava";
import htmlToAbsUrls from "../HtmlToAbsoluteUrls.js";
test("Changes a link href", async t => {
t.is((await htmlToAbsUrls(`<a href="#testanchor">Hello</a>`, "http://example.com/")).html, `<a href="#testanchor">Hello</a>`);
t.is((await htmlToAbsUrls(`<a href="/test.html">Hello</a>`, "http://example.com/")).html, `<a href="http://example.com/test.html">Hello</a>`);
t.is((await htmlToAbsUrls(`<img src="/test.png">`, "http://example.com/")).html, `<img src="http://example.com/test.png">`);
t.is((await htmlToAbsUrls(`<a href="http://someotherdomain/">Hello</a>`, "http://example.com/")).html, `<a href="http://someotherdomain/">Hello</a>`);
});

View File

@ -22,21 +22,10 @@
},
"homepage": "https://github.com/11ty/eleventy-base-blog#readme",
"dependencies": {
"@11ty/eleventy": "0.2.12",
"@11ty/eleventy": "0.2.13",
"@11ty/eleventy-plugin-rss": "^1.0.1",
"@11ty/eleventy-plugin-syntaxhighlight": "^1.0.0",
"luxon": "^0.3.1",
"posthtml": "^0.11.2",
"posthtml-urls": "^1.0.0",
"prismjs": "^1.10.0"
},
"devDependencies": {
"ava": "^0.25.0"
},
"ava": {
"files": [
"_src/test/*.js"
],
"source": [
"_src/**/*.{js,jsx}"
]
}
}

View File

@ -11,10 +11,10 @@ Bring to the table win-win survival strategies to ensure proactive domination. A
Capitalize on low hanging fruit to identify a ballpark value added activity to beta test. Override the digital divide with additional clickthroughs from DevOps. Nanotechnology immersion along the information highway will close the loop on focusing solely on the bottom line.
{% highlight js 2-3 %}
{% highlight-plain js 2-3 %}
// this is a command
function myCommand() {
let counter = 0;
counter++;
}
{% endhighlight %}
{% endhighlight %}

View File

@ -7,6 +7,9 @@ layout: layouts/post.njk
---
Leverage agile frameworks to provide a robust synopsis for high level overviews. Iterative approaches to corporate strategy foster collaborative thinking to further the overall value proposition. Organically grow the holistic world view of disruptive innovation via workplace diversity and empowerment.
<a href="{{ '/posts/firstpost/' | url }}">First post</a>
<a href="{{ '/posts/thirdpost/' | url }}">Third post</a>
Bring to the table win-win survival strategies to ensure proactive domination. At the end of the day, going forward, a new normal that has evolved from generation X is on the runway heading towards a streamlined cloud solution. User generated content in real-time will have multiple touchpoints for offshoring.
Capitalize on low hanging fruit to identify a ballpark value added activity to beta test. Override the digital divide with additional clickthroughs from DevOps. Nanotechnology immersion along the information highway will close the loop on focusing solely on the bottom line.
Capitalize on low hanging fruit to identify a ballpark value added activity to beta test. Override the digital divide with additional clickthroughs from DevOps. Nanotechnology immersion along the information highway will close the loop on focusing solely on the bottom line.