2023-06-19 23:55:26 +02:00
|
|
|
import {LoremIpsum} from 'lorem-ipsum';
|
2023-06-22 19:06:19 +02:00
|
|
|
import stripInd from 'strip-indent';
|
2023-03-10 22:44:54 +01:00
|
|
|
import truncate from 'truncate-html';
|
2023-06-19 23:55:26 +02:00
|
|
|
import {typogrify} from 'typogr';
|
2023-03-08 21:59:19 +01:00
|
|
|
|
2023-06-19 23:55:26 +02:00
|
|
|
import {markdownEngine} from './engines';
|
2023-03-08 21:59:19 +01:00
|
|
|
|
|
|
|
const lorem = new LoremIpsum();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns block of generated `lorem ipsum` text.
|
|
|
|
*
|
|
|
|
* @see https://github.com/knicklabs/lorem-ipsum.js
|
|
|
|
*/
|
|
|
|
export const getLorem = (type: string, number = 1) => {
|
|
|
|
switch (type) {
|
|
|
|
case 'sentence':
|
|
|
|
case 'sentences':
|
|
|
|
return lorem.generateSentences(number);
|
|
|
|
case 'paragraph':
|
|
|
|
case 'paragraphs':
|
|
|
|
return lorem.generateParagraphs(number);
|
|
|
|
case 'word':
|
|
|
|
case 'words':
|
|
|
|
return lorem.generateWords(number);
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
2023-06-22 19:06:19 +02:00
|
|
|
/**
|
|
|
|
* Strips leading whitespace from each line of a string.
|
|
|
|
* First strips based on the line with the least amount of leading whitespace,
|
|
|
|
* then strips based on the whitespace of the first line.
|
|
|
|
*
|
|
|
|
* @see https://github.com/sindresorhus/strip-indent
|
|
|
|
*/
|
|
|
|
export const stripIndent = (contents: string) => {
|
|
|
|
// Strip leading whitespace based on line with least leading whitespace
|
|
|
|
let text = stripInd(contents);
|
|
|
|
// Find leading whitespace of first line (ignoring initial newlines)
|
|
|
|
const match = text.replace(/^[\n\r]/, '').match(/^[ \t]*(?=\S)/g);
|
|
|
|
if (match?.[0]?.length) {
|
|
|
|
// Strip leading whitespace based on first line
|
|
|
|
text = text.replaceAll(new RegExp(`^[ \\t]{${match[0].length}}`, 'gm'), '');
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
2023-03-10 22:44:54 +01:00
|
|
|
/**
|
|
|
|
* Truncates an HTML string without breaking tags.
|
|
|
|
*
|
|
|
|
* @see https://github.com/oe/truncate-html
|
|
|
|
*/
|
|
|
|
export const truncateHTML = (html: string, words = 170) =>
|
2023-06-19 23:55:26 +02:00
|
|
|
truncate(html, words, {byWords: true, keepWhitespaces: true});
|
2023-03-10 22:44:54 +01:00
|
|
|
|
2023-03-08 21:59:19 +01:00
|
|
|
/**
|
|
|
|
* Renders block of Markdown into HTML.
|
|
|
|
*/
|
2023-05-31 16:06:00 +02:00
|
|
|
export const markdown = (content: string) =>
|
|
|
|
markdownEngine.render(stripIndent(content));
|
2023-03-08 21:59:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders single line of Markdown into HTML, without wrapping `<p>`.
|
|
|
|
*/
|
|
|
|
export const markdownInline = (content: string) =>
|
|
|
|
markdownEngine.renderInline(content);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies various transformations to plain text in order to yield
|
|
|
|
* typographically-improved HTML.
|
|
|
|
*
|
|
|
|
* @see https://github.com/ekalinin/typogr.js
|
|
|
|
*/
|
|
|
|
export const typogr = (content: string) => typogrify(content);
|
2023-03-09 23:32:49 +01:00
|
|
|
|
2023-03-10 22:44:54 +01:00
|
|
|
/**
|
|
|
|
* Appends full page URL to internal links (for embedding in another page).
|
|
|
|
*/
|
|
|
|
export const replaceInternalLinks = (content: string, url: string) =>
|
|
|
|
content.replace(/href="#/g, `href="${url}#`);
|
|
|
|
|
2023-05-24 18:54:23 +02:00
|
|
|
/**
|
|
|
|
* Checks if a given string starts with a comparison string.
|
|
|
|
*/
|
|
|
|
export const startsWith = (str: string, check: string) => str.startsWith(check);
|
|
|
|
|
2023-06-20 18:13:03 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2023-03-09 23:32:49 +01:00
|
|
|
export default function typePlugin(eleventyConfig: any) {
|
|
|
|
// filters...
|
2023-03-10 22:44:54 +01:00
|
|
|
eleventyConfig.addLiquidFilter('truncateHTML', truncateHTML);
|
2023-03-09 23:32:49 +01:00
|
|
|
eleventyConfig.addLiquidFilter('markdown', markdown);
|
|
|
|
eleventyConfig.addLiquidFilter('markdownInline', markdownInline);
|
|
|
|
eleventyConfig.addLiquidFilter('typogr', typogr);
|
2023-03-10 22:44:54 +01:00
|
|
|
eleventyConfig.addLiquidFilter('replaceInternalLinks', replaceInternalLinks);
|
2023-05-24 18:54:23 +02:00
|
|
|
eleventyConfig.addLiquidFilter('startsWith', startsWith);
|
2023-03-09 23:32:49 +01:00
|
|
|
|
|
|
|
// shortcodes...
|
|
|
|
eleventyConfig.addLiquidShortcode('lorem', getLorem);
|
|
|
|
|
|
|
|
// paired shortcodes...
|
|
|
|
eleventyConfig.addPairedLiquidShortcode('markdown', markdown);
|
|
|
|
eleventyConfig.addPairedLiquidShortcode('typogr', typogr);
|
|
|
|
}
|