Skip to content

Commit

Permalink
[feat]: fix bug with shipwright not defined (#9)
Browse files Browse the repository at this point in the history
* fix(plugin-sails-content): refactor call to generateContent

* feat(sails-hook-content): set shipwright methods as locals

* chore(plugin-sails-content): reinstall ejs

* feat(plugin-sails-content): remove unneeded check for inputDir

* feat(plugin-sails-content): revert to using ejs to render
  • Loading branch information
DominusKelvin authored Jul 31, 2024
1 parent 32e9d51 commit af69ec3
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 29 deletions.
13 changes: 2 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions packages/plugin-sails-content/lib/generate-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
12 changes: 5 additions & 7 deletions packages/plugin-sails-content/lib/index.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
12 changes: 6 additions & 6 deletions packages/plugin-sails-content/lib/render.js
Original file line number Diff line number Diff line change
@@ -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
})
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-sails-content/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"author": "Kelvin Omereshone <kelvin@sailscasts.com>",
"license": "MIT",
"dependencies": {
"ejs": "^3.1.10",
"gray-matter": "^4.0.3",
"showdown": "^2.1.0"
}
Expand Down
13 changes: 12 additions & 1 deletion packages/sails-hook-content/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
},
Expand All @@ -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)
)
}
}
}
Expand Down

0 comments on commit af69ec3

Please sign in to comment.