Commit d41e80d7 authored by julientaq's avatar julientaq
Browse files

Merge branch 'main' of gitlab.coko.foundation:kotahi/kotahi-default-pub

parents ea610635 9d2da2e5
Pipeline #36691 passed with stages
in 1 minute and 9 seconds
......@@ -5,32 +5,59 @@ const pluginTOC = require("eleventy-plugin-nesting-toc");
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
const { DateTime } = require("luxon");
const cheerio = require('cheerio');
const flatten = require('flat')
const fs = require('fs')
const fg = require('fast-glob');
const cheerio = require("cheerio");
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const flatten = require("flat");
const fs = require("fs");
const path = require("path")
const https = require("https");
const fg = require("fast-glob");
module.exports = function (eleventyConfig) {
// collection
eleventyConfig.addFilter("imagesHandler", function (value, id) {
fs.mkdir(path.join(__dirname, 'public/images/articles'), (err) => {
if (err) {
return console.error(err);
}
console.log('Directory created successfully!');
});
const article = new JSDOM(value);
article.window.document.body.querySelectorAll("img").forEach((img) => {
https
.get(img.src, (res) => {
const file = fs.createWriteStream(
`public/images/articles/art${id}-${img.alt}`
);
res.pipe(file);
file.on("finish", () => {
file.close();
console.log(`${img.src} has been downloaded!`);
});
})
.on("error", (err) => {
console.log("Error: ", err.message);
});
img.src = `/images/articles/art${id}-${img.alt}`;
});
return article.window.document.body.innerHTML;
});
eleventyConfig.addCollection("sortedByOrder", function (collectionApi) {
return collectionApi.getAll().sort((a, b) => {
if (a.data.order > b.data.order) return 1;
else if (a.data.order < b.data.order) return -1;
else return 0;
});
});
eleventyConfig.addFilter("filterHTML", (str) =>
str.replace(/(<([^>]+)>)/gi, " ")
);
eleventyConfig.addFilter("filterHTML",str => str.replace( /(<([^>]+)>)/ig, ' '))
eleventyConfig.addPassthroughCopy({"static/css": "/css"});
eleventyConfig.addPassthroughCopy({ "static/css": "/css" });
eleventyConfig.addPassthroughCopy({ "static/fonts": "/fonts" });
eleventyConfig.addPassthroughCopy({ "static/js": "/js" });
eleventyConfig.addPassthroughCopy({ "static/images": "/images" });
......@@ -50,16 +77,19 @@ module.exports = function (eleventyConfig) {
// useful to use the toc somewhere else
eleventyConfig.addFilter("prependLinks", function (value, prepend) {
return value.replace(/<a href="/g, `<a href="${prepend}`)
});
eleventyConfig.addFilter("replaceWithRegex", function (replaceThat, replaceWith) {
let regex = new RegExp(replaceThat);
return value.replace(regex, replaceWith)
return value.replace(/<a href="/g, `<a href="${prepend}`);
});
eleventyConfig.addFilter(
"replaceWithRegex",
function (replaceThat, replaceWith) {
let regex = new RegExp(replaceThat);
return value.replace(regex, replaceWith);
}
);
eleventyConfig.addFilter("cleanLink", function (value) {
let regex = new RegExp('');
return value.replace(/static\/outputs\/\d+?\//, "")
let regex = new RegExp("");
return value.replace(/static\/outputs\/\d+?\//, "");
});
// add latin number plugin
......@@ -67,20 +97,24 @@ module.exports = function (eleventyConfig) {
return romanize(value);
});
// add link to the diverses files
const supplementary = fg.sync(['**/outputs/**', '!**/node_modules/', '!**/temp', '!**/public',]);
eleventyConfig.addCollection('supplementaryFiles', function (collection) {
// add link to the diverses files
const supplementary = fg.sync([
"**/outputs/**",
"!**/node_modules/",
"!**/temp",
"!**/public",
]);
eleventyConfig.addCollection("supplementaryFiles", function (collection) {
return supplementary;
});
// \get the date with luxon (for all date)
eleventyConfig.addFilter("postDate", (dateObj) => {
let date = new Date(dateObj)
let date = new Date(dateObj);
return DateTime.fromJSDate(date).toLocaleString(DateTime.DATE_MED);
});
// limit the amount of items
eleventyConfig.addFilter("limit", function (arr, limit) {
return arr.slice(0, limit);
......@@ -90,22 +124,20 @@ module.exports = function (eleventyConfig) {
// console.log(value);
const $ = cheerio.load(value);
if ($.html(el)) {
return value = $.html(el);
}
else {
return (value = $.html(el));
} else {
return value;
}
});
eleventyConfig.addFilter("addIDtoTitles", function (value) {
// cheerio only accept string, no html buffer :shrug:
// cheerio only accept string, no html buffer :shrug:
const $ = cheerio.load(`${value}`);
$('h2,h3,h4,h5').each(function (i, elem) {
$(this).attr('id', $(this).text().toLowerCase().replace(/\s/g, ''));
})
$("h2,h3,h4,h5").each(function (i, elem) {
$(this).attr("id", $(this).text().toLowerCase().replace(/\s/g, ""));
});
// add it to figure, seems to break because of the base64
// doesnt work with base64 as the content is way too big
......@@ -113,20 +145,11 @@ module.exports = function (eleventyConfig) {
// $(this).attr('id', `fig-${i})`);
// })
return $.html();
});
})
eleventyConfig.addFilter('showAvailableMeta', function (value) {
return propertiesToArray(value)
})
eleventyConfig.addFilter("showAvailableMeta", function (value) {
return propertiesToArray(value);
});
eleventyConfig.addPlugin(pluginTOC, {
tags: ["h2", "h3", "h4"], // which heading tags are selected headings must each have an ID attribute
......@@ -136,11 +159,6 @@ module.exports = function (eleventyConfig) {
flat: false,
});
// folder structures
// -----------------------------------------------------------------------------
// content, data and layouts comes from the src folders
......@@ -156,41 +174,63 @@ module.exports = function (eleventyConfig) {
};
};
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function romanize(num) {
// taken from Steven Levithan
// https://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter
if (isNaN(num))
return NaN;
if (isNaN(num)) return NaN;
var digits = String(+num).split(""),
key = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM",
"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC",
"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"],
key = [
"",
"C",
"CC",
"CCC",
"CD",
"D",
"DC",
"DCC",
"DCCC",
"CM",
"",
"X",
"XX",
"XXX",
"XL",
"L",
"LX",
"LXX",
"LXXX",
"XC",
"",
"I",
"II",
"III",
"IV",
"V",
"VI",
"VII",
"VIII",
"IX",
],
roman = "",
i = 3;
while (i--)
roman = (key[+digits.pop() + (i * 10)] || "") + roman;
while (i--) roman = (key[+digits.pop() + i * 10] || "") + roman;
return Array(+digits.join("") + 1).join("M") + roman;
}
// to debug → get all the property of an element as a flat HTML
function propertiesToArray(value) {
let stuff = flatten(value, { maxDepth: 10 });
let content = "";
for (var key in stuff) {
content += `<section><div class="meta">${key}</div><div class="value">${stuff[key] != null ? stuff[key] : ''}</div></section>`
content += `<section><div class="meta">${key}</div><div class="value">${
stuff[key] != null ? stuff[key] : ""
}</div></section>`;
}
return content;
}
This diff is collapsed.
......@@ -134,9 +134,10 @@
</section>
<section id="article" class="article">
{{
article.meta.source | addIDtoTitles | safe
article.meta.source | addIDtoTitles | imagesHandler(article.shortId) | safe
}}
</section>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment