@@ -5,6 +5,8 @@ const { validateTemplate } = require("./utils");
5
5
const { createLayoutLayers } = require ( "./layout" ) ;
6
6
const { createFontsMapFromTemplates } = require ( "./font" ) ;
7
7
const { createImagesMapFromTemplates } = require ( "./image" ) ;
8
+ const { getConfig } = require ( "./config" ) ;
9
+ const { getTemplateNameByRules } = require ( "./rules" ) ;
8
10
9
11
module . exports = function ( context , { templatesDir } ) {
10
12
const isProd = process . env . NODE_ENV === "production" ;
@@ -17,33 +19,40 @@ module.exports = function (context, { templatesDir }) {
17
19
18
20
const templates = getTemplates ( templatesDir ) ;
19
21
if ( ! templates . some ( validateTemplate ) ) {
20
- console . error ( "Template validation error." ) ;
21
22
return ;
22
23
}
23
24
25
+ const config = getConfig ( templatesDir ) ;
26
+ if ( ! config ) return ;
27
+
28
+ // TODO: File not found exception?
24
29
const fonts = createFontsMapFromTemplates ( templates ) ;
25
30
const images = createImagesMapFromTemplates ( templates ) ;
26
31
27
32
async function generateImageFromDoc ( doc , locale , outputDir ) {
28
33
const { id, title } = doc ;
29
34
30
- // use basic
31
35
const hashFileName = sha1 ( id + locale ) ;
36
+ const templateName = getTemplateNameByRules ( id , config . rules ) ;
32
37
33
- const template = templates . find ( ( item ) => item . name === "basic" ) ;
38
+ const template = templates . find ( ( item ) => item . name === templateName ) ;
34
39
35
40
const previewImage = await images . get ( `${ template . name } _${ template . params . image } ` ) . clone ( ) ;
36
41
37
42
const previewFont = fonts . get ( template . params . font ) ;
38
43
39
- const textLayers = createLayoutLayers ( doc , template . params . layout , previewFont ) ;
44
+ const textLayers = createLayoutLayers (
45
+ doc ,
46
+ template . params . layout ,
47
+ previewFont ,
48
+ config . textWidthLimit ,
49
+ ) ;
40
50
41
51
try {
42
52
await previewImage . composite ( textLayers ) ;
43
53
await previewImage
44
54
. jpeg ( {
45
- // TODO: Quality from config.json
46
- quality : 80 ,
55
+ quality : config . quality ,
47
56
chromaSubsampling : "4:4:4" ,
48
57
} )
49
58
. toFile ( `${ outputDir } \\${ hashFileName } .jpg` ) ;
@@ -59,7 +68,7 @@ module.exports = function (context, { templatesDir }) {
59
68
( plugin ) => plugin . name === "docusaurus-plugin-content-docs" ,
60
69
) ;
61
70
62
- const previewOutputDir = `${ outDir } \\assets\\og\\ ` ;
71
+ const previewOutputDir = `${ outDir } \\${ config . outputDir } ` ;
63
72
fs . mkdir ( previewOutputDir , { recursive : true } , ( error ) => {
64
73
if ( error ) throw error ;
65
74
} ) ;
0 commit comments