From 64ff9eabeb574aed636e651fd31f259633729dea Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Tue, 16 Jan 2024 12:45:48 +0100 Subject: [PATCH 1/7] [feat] add global content config (#3) * feat(plugin-sails-content): update config to use an object to take layout as well * feat(plugin-sails-content): update pluginSailsContent to take a config object * feat(plugin-sails-content): add global layout config * feat(sails-hook-content): pass the content config to pluginSailsContent --- .../lib/generate-content.js | 17 +++++++++++------ packages/plugin-sails-content/lib/index.js | 6 +++--- packages/plugin-sails-content/lib/render.js | 6 ++++-- packages/sails-hook-content/lib/index.js | 2 +- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/packages/plugin-sails-content/lib/generate-content.js b/packages/plugin-sails-content/lib/generate-content.js index 183bde0..c6025ba 100644 --- a/packages/plugin-sails-content/lib/generate-content.js +++ b/packages/plugin-sails-content/lib/generate-content.js @@ -3,22 +3,27 @@ const path = require('path') const render = require('./render') const writeHtmlToOutput = require('./write-html-to-output') -async function generateContent(inputDir, outputDir = '.tmp/public') { - const files = await fs.readdir(inputDir) +async function generateContent(config) { + config.outputDir = config.outputDir ? config.outputDir : '.tmp/public' + const files = await fs.readdir(config.inputDir) for (const file of files) { - const filePath = path.join(inputDir, file) + const filePath = path.join(config.inputDir, file) const fileStats = await fs.stat(filePath) if (fileStats.isDirectory()) { - await generateContent(filePath, path.join(outputDir, file)) + await generateContent({ + inputDir: filePath, + outputDir: path.join(config.outputDir, file) + }) } else if (fileStats.isFile() && file.toLowerCase().endsWith('.md')) { - const { data, renderedHtml } = await render(filePath) + const { data, renderedHtml } = await render(filePath, config.layout) const outputFilePath = await writeHtmlToOutput( renderedHtml, file, - inputDir + config.inputDir ) + // @ts-ignore sails.log.verbose( `[sails:content] Processed ${filePath} -> ${outputFilePath}` ) diff --git a/packages/plugin-sails-content/lib/index.js b/packages/plugin-sails-content/lib/index.js index 7f45a4a..1c916d0 100644 --- a/packages/plugin-sails-content/lib/index.js +++ b/packages/plugin-sails-content/lib/index.js @@ -1,13 +1,13 @@ const generateContent = require('./generate-content') -module.exports = function pluginSailsContent(options) { +module.exports = function pluginSailsContent(config) { return { name: 'sails:content', setup(api) { api.onDevCompileDone(async function () { - await generateContent(options.inputDir) + await generateContent(config) }) api.onAfterBuild(async function () { - await generateContent(options.inputDir) + await generateContent(config) }) } } diff --git a/packages/plugin-sails-content/lib/render.js b/packages/plugin-sails-content/lib/render.js index 5e48935..51ee085 100644 --- a/packages/plugin-sails-content/lib/render.js +++ b/packages/plugin-sails-content/lib/render.js @@ -3,14 +3,16 @@ const matter = require('gray-matter') const showdown = require('showdown') const ejs = require('ejs') -async function render(mdFile) { +async function render(mdFile, layout) { const fileContent = await fs.readFile(mdFile, { encoding: 'utf8' }) const { data, content } = matter(fileContent) + layout = layout || data.layout + const converter = new showdown.Converter({ ghCompatibleHeaderId: true }) const htmlContent = converter.makeHtml(content) - const layoutContent = await fs.readFile(data.layout, { encoding: 'utf8' }) + const layoutContent = await fs.readFile(layout, { encoding: 'utf8' }) const renderedHtml = ejs.render(layoutContent, { data, content: htmlContent }) diff --git a/packages/sails-hook-content/lib/index.js b/packages/sails-hook-content/lib/index.js index ec0033b..aeab111 100644 --- a/packages/sails-hook-content/lib/index.js +++ b/packages/sails-hook-content/lib/index.js @@ -20,7 +20,7 @@ module.exports = function defineSailsContentHook(sails) { sails.log.info('Initializing custom hook (`sails-content`)') if (sails.config.content.output == 'static') { sails.config.shipwright.build.plugins.push( - pluginSailsContent({ inputDir: sails.config.content.inputDir }) + pluginSailsContent(sails.config.content) ) } } From 98e317d350acc235ffbfacf875ddd8f74e936b2d Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Tue, 16 Jan 2024 12:47:51 +0100 Subject: [PATCH 2/7] chore(plugin-sails-content): release 0.0.3 --- packages/plugin-sails-content/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-sails-content/package.json b/packages/plugin-sails-content/package.json index 7a736a6..563dd58 100644 --- a/packages/plugin-sails-content/package.json +++ b/packages/plugin-sails-content/package.json @@ -1,6 +1,6 @@ { "name": "plugin-sails-content", - "version": "0.0.2", + "version": "0.0.3", "description": "Sails Content plugin for Rsbuild", "main": "lib/index.js", "scripts": { From 23e3ffa1207921a88ad11c7f6fde58eb42d40b65 Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Tue, 16 Jan 2024 12:48:23 +0100 Subject: [PATCH 3/7] chore(sails-hook-content): release 0.0.3 --- packages/sails-hook-content/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sails-hook-content/package.json b/packages/sails-hook-content/package.json index 76d640f..3fa5f96 100644 --- a/packages/sails-hook-content/package.json +++ b/packages/sails-hook-content/package.json @@ -1,6 +1,6 @@ { "name": "sails-hook-content", - "version": "0.0.2", + "version": "0.0.3", "description": "Sails hook for Sails Content", "main": "lib/index.js", "scripts": { From fe3adacfe662320e2dd739c51204ac13418cf454 Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Tue, 16 Jan 2024 13:08:28 +0100 Subject: [PATCH 4/7] chore(plugin-sails-content): release 0.0.4 --- packages/plugin-sails-content/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-sails-content/package.json b/packages/plugin-sails-content/package.json index 563dd58..31f8a9c 100644 --- a/packages/plugin-sails-content/package.json +++ b/packages/plugin-sails-content/package.json @@ -1,6 +1,6 @@ { "name": "plugin-sails-content", - "version": "0.0.3", + "version": "0.0.4", "description": "Sails Content plugin for Rsbuild", "main": "lib/index.js", "scripts": { From c66b9279c762740409c8609b33f8a3088cdbb100 Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Tue, 16 Jan 2024 13:06:14 +0100 Subject: [PATCH 5/7] fix(plugin-sails-content): pass global config --- packages/plugin-sails-content/lib/generate-content.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/plugin-sails-content/lib/generate-content.js b/packages/plugin-sails-content/lib/generate-content.js index c6025ba..9531c4d 100644 --- a/packages/plugin-sails-content/lib/generate-content.js +++ b/packages/plugin-sails-content/lib/generate-content.js @@ -13,6 +13,7 @@ async function generateContent(config) { if (fileStats.isDirectory()) { await generateContent({ + ...config, inputDir: filePath, outputDir: path.join(config.outputDir, file) }) From e1526e6050ff6fae3bd032abb1e0558d2d4c55d5 Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Tue, 16 Jan 2024 13:15:07 +0100 Subject: [PATCH 6/7] chore: update package-lock.json --- package-lock.json | 6 +++--- packages/sails-hook-content/package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 432caae..003c0e5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3352,7 +3352,7 @@ } }, "packages/plugin-sails-content": { - "version": "0.0.2", + "version": "0.0.4", "license": "MIT", "dependencies": { "ejs": "^3.1.9", @@ -3366,10 +3366,10 @@ "devDependencies": {} }, "packages/sails-hook-content": { - "version": "0.0.1", + "version": "0.0.3", "license": "MIT", "dependencies": { - "plugin-sails-content": "^0.0.2" + "plugin-sails-content": "^0.0.4" } } } diff --git a/packages/sails-hook-content/package.json b/packages/sails-hook-content/package.json index 3fa5f96..0e423bc 100644 --- a/packages/sails-hook-content/package.json +++ b/packages/sails-hook-content/package.json @@ -13,6 +13,6 @@ "hookName": "content" }, "dependencies": { - "plugin-sails-content": "^0.0.2" + "plugin-sails-content": "^0.0.4" } } From 37b4fc7e297118ae0c52f1284e60d243df5ebc89 Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Tue, 16 Jan 2024 13:23:16 +0100 Subject: [PATCH 7/7] chore(sails-hook-content): release 0.0.4 --- packages/sails-hook-content/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sails-hook-content/package.json b/packages/sails-hook-content/package.json index 0e423bc..aadfabd 100644 --- a/packages/sails-hook-content/package.json +++ b/packages/sails-hook-content/package.json @@ -1,6 +1,6 @@ { "name": "sails-hook-content", - "version": "0.0.3", + "version": "0.0.4", "description": "Sails hook for Sails Content", "main": "lib/index.js", "scripts": {