|
| 1 | +const fs = require("fs"); |
| 2 | +const sha1 = require("sha1"); |
| 3 | +const { getTemplates } = require("./template"); |
| 4 | +const { createLayoutLayers } = require("./layout"); |
| 5 | +const { createFontsMapFromTemplates } = require("./font"); |
| 6 | +const { createImagesMapFromTemplates, getTemplateImageId } = require("./image"); |
| 7 | +const { getConfig } = require("./config"); |
| 8 | +const { getTemplateNameByRules } = require("./rules"); |
| 9 | + |
| 10 | +module.exports = function ({ templatesDir }) { |
| 11 | + const initData = bootstrap(templatesDir); |
| 12 | + if (!initData) { |
| 13 | + console.error("OpenGraph plugin exit with error."); |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + const { config } = initData; |
| 18 | + |
| 19 | + return { |
| 20 | + name: "docusaurus-plugin-open-graph-image", |
| 21 | + async postBuild({ plugins, outDir, i18n }) { |
| 22 | + const docsPlugin = plugins.find( |
| 23 | + (plugin) => plugin.name === "docusaurus-plugin-content-docs", |
| 24 | + ); |
| 25 | + |
| 26 | + if (!docsPlugin) throw new Error("Docusaurus Doc plugin not found."); |
| 27 | + |
| 28 | + const previewOutputDir = `${outDir}\\${config.outputDir}`; |
| 29 | + fs.mkdir(previewOutputDir, { recursive: true }, (error) => { |
| 30 | + if (error) throw error; |
| 31 | + }); |
| 32 | + |
| 33 | + const docsContent = docsPlugin.content; |
| 34 | + const docsVersions = docsContent.loadedVersions; |
| 35 | + docsVersions.forEach((version) => { |
| 36 | + const { docs } = version; |
| 37 | + |
| 38 | + docs.forEach((document) => { |
| 39 | + generateImageFromDoc(initData, document, i18n.currentLocale, previewOutputDir); |
| 40 | + }); |
| 41 | + }); |
| 42 | + }, |
| 43 | + }; |
| 44 | +}; |
| 45 | + |
| 46 | +function bootstrap(templatesDir) { |
| 47 | + const isProd = process.env.NODE_ENV === "production"; |
| 48 | + if (!isProd) return; |
| 49 | + |
| 50 | + if (!templatesDir) { |
| 51 | + console.error("Wrong templatesDir option."); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + const templates = getTemplates(templatesDir); |
| 56 | + if (!templates) return; |
| 57 | + |
| 58 | + const config = getConfig(templatesDir); |
| 59 | + if (!config) return; |
| 60 | + |
| 61 | + // TODO: File not found exception? |
| 62 | + const fonts = createFontsMapFromTemplates(templates); |
| 63 | + const images = createImagesMapFromTemplates(templates); |
| 64 | + |
| 65 | + return { templates, config, fonts, images }; |
| 66 | +} |
| 67 | + |
| 68 | +async function generateImageFromDoc(initData, doc, locale, outputDir) { |
| 69 | + const { templates, config, images, fonts } = initData; |
| 70 | + const { id, title } = doc; |
| 71 | + |
| 72 | + const hashFileName = sha1(id + locale); |
| 73 | + |
| 74 | + const templateName = getTemplateNameByRules(id, config.rules); |
| 75 | + |
| 76 | + const template = templates.find((template) => template.name === templateName); |
| 77 | + |
| 78 | + const previewImage = await images.get(getTemplateImageId(template)).clone(); |
| 79 | + |
| 80 | + const previewFont = fonts.get(template.params.font); |
| 81 | + |
| 82 | + const textLayers = createLayoutLayers( |
| 83 | + doc, |
| 84 | + template.params.layout, |
| 85 | + previewFont, |
| 86 | + config.textWidthLimit, |
| 87 | + ); |
| 88 | + |
| 89 | + try { |
| 90 | + await previewImage.composite(textLayers); |
| 91 | + await previewImage |
| 92 | + .jpeg({ |
| 93 | + quality: config.quality, |
| 94 | + chromaSubsampling: "4:4:4", |
| 95 | + }) |
| 96 | + .toFile(`${outputDir}\\${hashFileName}.jpg`); |
| 97 | + } catch (error) { |
| 98 | + console.error(error, id, title, hashFileName); |
| 99 | + } |
| 100 | +} |
0 commit comments