1 Star 0 Fork 0

wangchito/JS单行代码

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
.eleventy.js 3.26 KB
一键复制 编辑 原始数据 按行查看 历史
Phuoc Nguyen 提交于 2021-05-31 18:22 . Sort snippets by title
const pluginRss = require('@11ty/eleventy-plugin-rss');
const pluginSyntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const pluginNavigation = require('@11ty/eleventy-navigation');
const markdownIt = require('markdown-it');
const minify = require('html-minifier').minify;
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(pluginSyntaxHighlight);
eleventyConfig.addPlugin(pluginNavigation);
eleventyConfig.addPassthroughCopy('assets');
eleventyConfig.addPassthroughCopy('css');
eleventyConfig.addPassthroughCopy('js');
eleventyConfig.addWatchTarget('./css/');
eleventyConfig.addWatchTarget('./js/');
let markdownLibrary = markdownIt({
html: true,
breaks: true,
linkify: true,
});
eleventyConfig.setLibrary('md', markdownLibrary);
eleventyConfig.addCollection('posts', (collection) => {
const posts = collection.getFilteredByTag('posts');
for(let i = 0; i < posts.length; i++) {
posts[i].data['index'] = i;
posts[i].data['prevPost'] = i === 0 ? null : posts[i - 1];
posts[i].data['nextPost'] = i === posts.length - 1 ? null : posts[i + 1];
}
return posts;
});
eleventyConfig.addCollection('postsByCategory', (collection) => {
const posts = collection.getFilteredByTag('posts').sort((a, b) => a.data.title.localeCompare(b.data.title));
const groups = posts.reduce((acc, item) => ((acc[item.data.category] = [...(acc[item.data.category] || []), item]), acc), {});
return Object.keys(groups).sort().reduce((p, c) => (p[c] = groups[c], p), {});
});
eleventyConfig.addFilter('countKeys', (arr) => {
return Object.keys(arr).length;
});
eleventyConfig.addFilter('randomItems', (arr, count) => {
return arr.concat().reduce((p, _, __, arr) => (p[0] < count) ? [p[0] + 1, p[1].concat(arr.splice(Math.random() * arr.length | 0, 1))] : p, [0, []])[1];
});
eleventyConfig.addFilter('sortBy', (arr, key) => {
return arr.sort((a, b) => (a[key] > b[key]) ? 1 : ((a[key] < b[key]) ? -1 : 0));
});
eleventyConfig.addFilter('chunk', (arr, size) => {
return arr.reduce((acc, e, i) => (i % size ? acc[acc.length - 1].push(e) : acc.push([e]), acc), []);
});
// Minify HTML
const minifyHtml = (rawContent, outputPath) => (
(outputPath && outputPath.endsWith('.html'))
? minify(rawContent, {
removeAttributeQuotes: true,
collapseBooleanAttributes: true,
collapseWhitespace: true,
removeComments: true,
sortClassName: true,
sortAttributes: true,
html5: true,
decodeEntities: true,
removeOptionalTags: true,
})
: rawContent
);
eleventyConfig.addTransform("minifyHtml", minifyHtml);
return {
templateFormats: ['md', 'njk', 'html', 'liquid'],
markdownTemplateEngine: 'liquid',
htmlTemplateEngine: 'njk',
dataTemplateEngine: 'njk',
dir: {
input: '.',
includes: '_includes',
data: '_data',
output: '_site',
},
};
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/wangchito/js-single-line-code.git
[email protected]:wangchito/js-single-line-code.git
wangchito
js-single-line-code
JS单行代码
master

搜索帮助