diff --git a/package-lock.json b/package-lock.json index 7d7b25b..d2e0867 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3356,6 +3356,7 @@ "version": "0.0.6", "license": "MIT", "dependencies": { + "ejs": "^3.1.10", "gray-matter": "^4.0.3", "showdown": "^2.1.0" } @@ -3371,17 +3372,7 @@ "version": "0.0.7", "license": "MIT", "dependencies": { - "plugin-sails-content": "^0.0.5" - } - }, - "packages/sails-hook-content/node_modules/plugin-sails-content": { - "version": "0.0.5", - "resolved": "https://registry.npmjs.org/plugin-sails-content/-/plugin-sails-content-0.0.5.tgz", - "integrity": "sha512-u5/43RCzaXNQa495QpHTSksYQ5dANb/bdS2fV31SW4bn66nUH3fPlCpn8RQFed1kfKjMVHBO74tOMVawpZs5BQ==", - "dependencies": { - "ejs": "^3.1.9", - "gray-matter": "^4.0.3", - "showdown": "^2.1.0" + "plugin-sails-content": "^0.0.6" } } } diff --git a/packages/plugin-sails-content/lib/generate-content.js b/packages/plugin-sails-content/lib/generate-content.js index 414f1f6..1738024 100644 --- a/packages/plugin-sails-content/lib/generate-content.js +++ b/packages/plugin-sails-content/lib/generate-content.js @@ -3,9 +3,7 @@ const path = require('path') const render = require('./render') const writeHtmlToOutput = require('./write-html-to-output') -async function generateContent(sails) { - const config = sails.config.content - config.outputDir = config.outputDir ? config.outputDir : '.tmp/public' +async function generateContent(config) { const files = await fs.readdir(config.inputDir) for (const file of files) { @@ -19,7 +17,7 @@ async function generateContent(sails) { outputDir: path.join(config.outputDir, file) }) } else if (fileStats.isFile() && file.toLowerCase().endsWith('.md')) { - const { renderedHtml } = await render(sails, filePath, config.layout) + const { renderedHtml } = await render(filePath, config) const outputFilePath = await writeHtmlToOutput( renderedHtml, diff --git a/packages/plugin-sails-content/lib/index.js b/packages/plugin-sails-content/lib/index.js index cc8185f..a9ed3d4 100644 --- a/packages/plugin-sails-content/lib/index.js +++ b/packages/plugin-sails-content/lib/index.js @@ -1,14 +1,12 @@ const generateContent = require('./generate-content') -module.exports = function pluginSailsContent(sails) { +module.exports = function pluginSailsContent(config) { return { name: 'sails:content', setup(api) { - api.onDevCompileDone(async function () { - await generateContent(sails) - }) - api.onAfterBuild(async function () { - await generateContent(sails) - }) + const runGenerateContent = async () => await generateContent(config) + + api.onDevCompileDone(runGenerateContent) + api.onAfterBuild(runGenerateContent) } } } diff --git a/packages/plugin-sails-content/lib/render.js b/packages/plugin-sails-content/lib/render.js index 15b30e7..8ea7ddf 100644 --- a/packages/plugin-sails-content/lib/render.js +++ b/packages/plugin-sails-content/lib/render.js @@ -1,20 +1,20 @@ const fs = require('fs/promises') const matter = require('gray-matter') const showdown = require('showdown') +const ejs = require('ejs') -async function render(sails, mdFile, layout) { - const fileContent = await fs.readFile(mdFile, { encoding: 'utf8' }) +async function render(filePath, config) { + const fileContent = await fs.readFile(filePath, { encoding: 'utf8' }) const { data, content } = matter(fileContent) - layout = layout || data.layout + const layout = data.layout || config.layout const converter = new showdown.Converter({ ghCompatibleHeaderId: true }) const htmlContent = converter.makeHtml(content) const layoutContent = await fs.readFile(layout, { encoding: 'utf8' }) - - const renderedHtml = await sails.renderView(layoutContent, { - layout: false, + const renderedHtml = ejs.render(layoutContent, { + ...config.locals, data, content: htmlContent }) diff --git a/packages/plugin-sails-content/package.json b/packages/plugin-sails-content/package.json index d60fa99..a7b32ac 100644 --- a/packages/plugin-sails-content/package.json +++ b/packages/plugin-sails-content/package.json @@ -21,6 +21,7 @@ "author": "Kelvin Omereshone ", "license": "MIT", "dependencies": { + "ejs": "^3.1.10", "gray-matter": "^4.0.3", "showdown": "^2.1.0" } diff --git a/packages/sails-hook-content/lib/index.js b/packages/sails-hook-content/lib/index.js index 53e61f2..2937e1f 100644 --- a/packages/sails-hook-content/lib/index.js +++ b/packages/sails-hook-content/lib/index.js @@ -10,6 +10,7 @@ module.exports = function defineSailsContentHook(sails) { defaults: { content: { inputDir: 'content', // Default content directory + outputDir: '.tmp/public', output: 'static' // creates the .html files for all your .md files in content at build time. } }, @@ -18,8 +19,18 @@ module.exports = function defineSailsContentHook(sails) { */ initialize: async function () { sails.log.info('Initializing custom hook (`sails-content`)') + if (sails.config.content.output == 'static') { - sails.config.shipwright.build.plugins.push(pluginSailsContent(sails)) + sails.config.content.locals = sails.config.views.locals || {} + + sails.config.content.locals.shipwright = { + scripts: sails.hooks.shipwright.generateScripts, + styles: sails.hooks.shipwright.generateStyles + } + + sails.config.shipwright.build.plugins.push( + pluginSailsContent(sails.config.content) + ) } } }