Skip to content

Commit ef2eb88

Browse files
committed
feat: config file validation.
1 parent 8225f9c commit ef2eb88

File tree

7 files changed

+75
-64
lines changed

7 files changed

+75
-64
lines changed

website/open-graph-templates/basic/template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"font": "arial.ttf",
44
"layout": [
55
{
6-
"type": "text",
6+
"typse": "text",
77
"name": "title",
88
"fontSize": 80,
99
"fill": "white",
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
22
"outputDir": "assets/og",
33
"textWidthLimit": 200,
4-
"rules": {
5-
"basic": {
4+
"rules": [
5+
{
6+
"name": "basic",
67
"priority": 0,
78
"pattern": "*"
89
},
9-
"gray": {
10+
{
11+
"name": "gray",
1012
"priority": 1,
1113
"pattern": "*"
1214
}
13-
}
15+
]
1416
}

website/plugins/docusaurus-plugin-open-graph-image/font.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
const textToSVG = require("text-to-svg");
22

3-
// TODO: save folder path to templates?
4-
module.exports.createFontsMapFromTemplates = function (templatesDir, templates) {
3+
module.exports.createFontsMapFromTemplates = function (templates) {
54
const fonts = new Map();
65
templates.forEach((item) => {
76
if (!fonts.has(item.params.font)) {
87
fonts.set(
98
item.params.font,
10-
textToSVG.loadSync(`${templatesDir}\\${item.name}\\${item.params.font}`),
9+
textToSVG.loadSync(`${item.path}\\${item.name}\\${item.params.font}`),
1110
);
1211
}
1312
});
1413
return fonts;
1514
};
1615

17-
module.exports.createSVGText = function createSVGText(
16+
module.exports.createSVGText = function (
1817
font,
1918
text,
2019
{ fontSize = 72, fill = "white", stroke = "white" },

website/plugins/docusaurus-plugin-open-graph-image/image.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@ function createImagePipeline(file) {
66
return sharp(file);
77
}
88

9-
// TODO: save folder path to templates?
10-
function createImageFromTemplate(templatesDir, { name, params }) {
11-
return createImagePipeline(`${templatesDir}\\${name}\\${params.image}`);
9+
function createImageFromTemplate({ path, name, params }) {
10+
return createImagePipeline(`${path}\\${name}\\${params.image}`);
1211
}
1312

14-
module.exports.createImagesMapFromTemplates = function (templatesDir, templates) {
13+
module.exports.createImagesMapFromTemplates = function (templates) {
1514
const images = new Map();
1615
templates.forEach((item) => {
1716
if (!images.has(item.params.image)) {
18-
images.set(
19-
`${item.name}_${item.params.image}`,
20-
createImageFromTemplate(templatesDir, item),
21-
);
17+
images.set(`${item.name}_${item.params.image}`, createImageFromTemplate(item));
2218
}
2319
});
2420
return images;

website/plugins/docusaurus-plugin-open-graph-image/index.js

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const fs = require("fs");
2-
const crypto = require("crypto");
2+
const sha1 = require("sha1");
33
const getTemplates = require("./template");
44
const { validateTemplate } = require("./utils");
55
const { createLayoutLayers } = require("./layout");
@@ -21,8 +21,36 @@ module.exports = function (context, { templatesDir }) {
2121
return;
2222
}
2323

24-
const fonts = createFontsMapFromTemplates(templatesDir, templates);
25-
const images = createImagesMapFromTemplates(templatesDir, templates);
24+
const fonts = createFontsMapFromTemplates(templates);
25+
const images = createImagesMapFromTemplates(templates);
26+
27+
async function generateImageFromDoc(doc, locale, outputDir) {
28+
const { id, title } = doc;
29+
30+
// use basic
31+
const hashFileName = sha1(id + locale);
32+
33+
const template = templates.find((item) => item.name === "basic");
34+
35+
const previewImage = await images.get(`${template.name}_${template.params.image}`).clone();
36+
37+
const previewFont = fonts.get(template.params.font);
38+
39+
const textLayers = createLayoutLayers(doc, template.params.layout, previewFont);
40+
41+
try {
42+
await previewImage.composite(textLayers);
43+
await previewImage
44+
.jpeg({
45+
// TODO: Quality from config.json
46+
quality: 80,
47+
chromaSubsampling: "4:4:4",
48+
})
49+
.toFile(`${outputDir}\\${hashFileName}.jpg`);
50+
} catch (error) {
51+
console.error(error, id, title, hashFileName);
52+
}
53+
}
2654

2755
return {
2856
name: "docusaurus-plugin-open-graph-image",
@@ -42,45 +70,8 @@ module.exports = function (context, { templatesDir }) {
4270
docsVersions.forEach((version) => {
4371
const { docs } = version;
4472

45-
async function generateImageFromDoc(doc) {
46-
const { id, title } = doc;
47-
48-
// use basic
49-
const template = templates.find((item) => item.name === "basic");
50-
51-
const previewImage = await images
52-
.get(`${template.name}_${template.params.image}`)
53-
.clone();
54-
55-
const previewFont = fonts.get(template.params.font);
56-
57-
const textLayers = createLayoutLayers(
58-
doc,
59-
template.params.layout,
60-
previewFont,
61-
);
62-
63-
await previewImage.composite(textLayers);
64-
65-
const shaOne = crypto.createHash("sha1");
66-
const hashFileName = shaOne.update(id + i18n.currentLocale).digest("hex");
67-
console.log("Generating: " + title + " " + id, hashFileName);
68-
69-
try {
70-
await previewImage
71-
.jpeg({
72-
// TODO: Quality from config.json
73-
quality: 80,
74-
chromaSubsampling: "4:4:4",
75-
})
76-
.toFile(`${previewOutputDir}\\${hashFileName}.jpg`);
77-
} catch (error) {
78-
console.log(error, id, title, hashFileName);
79-
}
80-
}
81-
8273
docs.forEach((item) => {
83-
generateImageFromDoc(item);
74+
generateImageFromDoc(item, i18n.currentLocale, previewOutputDir);
8475
});
8576
});
8677
}

website/plugins/docusaurus-plugin-open-graph-image/template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = exports = function getTemplates(templatesDir, encode = "utf8")
1010

1111
return templatesDirNames.map((templateName) => ({
1212
name: templateName,
13+
path: templatesDir,
1314
params: objectFromBuffer(
1415
fs.readFileSync(`${templatesDir}\\${templateName}\\template.json`, encode),
1516
),

website/plugins/docusaurus-plugin-open-graph-image/utils.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
const { number, string, is, object, array } = require("superstruct");
22

3-
const Template = object({
4-
image: string(),
5-
font: string(),
6-
layout: array(),
7-
});
8-
93
// TODO: May be with postEffects, images and etc? (optional fontSize, fill and etc)
104
const Layout = object({
115
type: string(),
@@ -17,6 +11,12 @@ const Layout = object({
1711
left: number(),
1812
});
1913

14+
const Template = object({
15+
image: string(),
16+
font: string(),
17+
layout: array(Layout),
18+
});
19+
2020
module.exports.validateTemplate = function ({ params }) {
2121
if (is(params, Template)) {
2222
if (params.layout.length === 0) return false;
@@ -32,3 +32,25 @@ module.exports.validateTemplate = function ({ params }) {
3232
module.exports.objectFromBuffer = function objectFromBuffer(buffer) {
3333
return JSON.parse(buffer.toString());
3434
};
35+
36+
const Config = object({
37+
outputDir: string(),
38+
textWidthLimit: number(),
39+
rules: array(),
40+
});
41+
42+
const Rule = object({
43+
name: string(),
44+
priority: number(),
45+
pattern: string(),
46+
});
47+
48+
module.exports.validateConfig = function (config) {
49+
if (is(config, Config)) {
50+
return config.rules.reduce((acc, item) => {
51+
if (!is(item, Rule)) return false;
52+
return acc;
53+
}, true);
54+
}
55+
return false;
56+
};

0 commit comments

Comments
 (0)