generated from 5t3ph/eleventy-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.eleventy.js
75 lines (64 loc) · 2.07 KB
/
.eleventy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const fs = require('fs');
const path = require('path');
const genSocialImage = require('./utils/generateSocialImage.js');
// Example use for the plugin:
// {% GenerateSocialImage 'Page title...', 'Website name' %}
// Defaults plugin config
const defaults = {
outputDir: './_site/img/preview',
urlPath: '/img/preview',
titleColor: '#FFF',
hideTerminal: false,
bgColor: '',
bgGradient: ['#647DEE', '#7F53AC'],
terminalBgColor: '#404040',
customSVG: '',
customFontFilename: '',
lineBreakAt: 35
};
module.exports = (eleventyConfig, options) => {
// Combine defaults with user defined options
const { outputDir, urlPath, titleColor, siteName, promoImage, hideTerminal, bgColor, bgGradient, terminalBgColor, customSVG, customFontFilename, lineBreakAt } = { ...defaults, ...options };
// Generate outputDir if it does not exist...
const sep = path.sep;
const targetDir = path.normalize(outputDir);
const initDir = path.isAbsolute(targetDir) ? sep : '';
targetDir.split(sep).reduce((parentDir, childDir) => {
const curDir = path.resolve(parentDir, childDir);
if (!fs.existsSync(curDir)) {
fs.mkdirSync(curDir);
}
return curDir;
}, initDir);
// Generate SVG Gradient...
let bgGradientDef = '';
if (bgGradient && bgGradient.length > 1) {
let colStops = ``;
let stopGap = Math.floor(100 / (bgGradient.length - 1));
for (let i = 0; i < bgGradient.length; i++ ) {
colStops += `<stop offset="${i * stopGap}%" stop-color="${bgGradient[i]}" />`;
}
bgGradientDef = `<linearGradient id="bg-gradient" x1="0" y1="0" x2="1" y2="1">${colStops}</linearGradient>`;
}
eleventyConfig.addAsyncShortcode("GenerateSocialImage", async (title) => {
if (!title) return '';
return await genSocialImage(
eleventyConfig.javascriptFunctions.slug(title), // file-name
title, // title
siteName, // site-name
promoImage, // promo-image
{ // options
targetDir,
urlPath,
titleColor,
hideTerminal,
bgColor,
bgGradientDef,
terminalBgColor,
customSVG,
customFontFilename,
lineBreakAt
}
);
});
};