Skip to content

Commit

Permalink
[feat] fix shipwright undefined in template (#8)
Browse files Browse the repository at this point in the history
* feat(sails-hook-content): pass sails instance to the Rsbuild plugin

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

* feat(plugin-sails-content): update render to use sails.renderView

* feat(plugin-sails-content): pass in sails instance to generateContent

* feat(plugin-sails-content): pass in sails to plugin hooks

* feat(plugin-sails-content): remove spread of sails views locals

* chore: update version
  • Loading branch information
DominusKelvin authored Jul 24, 2024
1 parent 0435c15 commit 8168c36
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 174 deletions.
164 changes: 3 additions & 161 deletions package-lock.json

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

8 changes: 5 additions & 3 deletions packages/plugin-sails-content/lib/generate-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const path = require('path')
const render = require('./render')
const writeHtmlToOutput = require('./write-html-to-output')

async function generateContent(config) {
async function generateContent(sails) {
const config = sails.config.content
config.outputDir = config.outputDir ? config.outputDir : '.tmp/public'
const files = await fs.readdir(config.inputDir)

Expand All @@ -18,13 +19,14 @@ async function generateContent(config) {
outputDir: path.join(config.outputDir, file)
})
} else if (fileStats.isFile() && file.toLowerCase().endsWith('.md')) {
const { data, renderedHtml } = await render(filePath, config.layout)
const { renderedHtml } = await render(sails, filePath, config.layout)

const outputFilePath = await writeHtmlToOutput(
renderedHtml,
file,
config.inputDir
)
// @ts-ignore

sails.log.verbose(
`[sails:content] Processed ${filePath} -> ${outputFilePath}`
)
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-sails-content/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const generateContent = require('./generate-content')
module.exports = function pluginSailsContent(config) {
module.exports = function pluginSailsContent(sails) {
return {
name: 'sails:content',
setup(api) {
api.onDevCompileDone(async function () {
await generateContent(config)
await generateContent(sails)
})
api.onAfterBuild(async function () {
await generateContent(config)
await generateContent(sails)
})
}
}
Expand Down
9 changes: 6 additions & 3 deletions packages/plugin-sails-content/lib/render.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const fs = require('fs/promises')
const matter = require('gray-matter')
const showdown = require('showdown')
const ejs = require('ejs')

async function render(mdFile, layout) {
async function render(sails, mdFile, layout) {
const fileContent = await fs.readFile(mdFile, { encoding: 'utf8' })
const { data, content } = matter(fileContent)

Expand All @@ -14,7 +13,11 @@ async function render(mdFile, layout) {

const layoutContent = await fs.readFile(layout, { encoding: 'utf8' })

const renderedHtml = ejs.render(layoutContent, { data, content: htmlContent })
const renderedHtml = await sails.renderView(layoutContent, {
layout: false,
data,
content: htmlContent
})

return { data, renderedHtml }
}
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-sails-content/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"author": "Kelvin Omereshone <[email protected]>",
"license": "MIT",
"dependencies": {
"ejs": "^3.1.9",
"gray-matter": "^4.0.3",
"showdown": "^2.1.0"
}
Expand Down
4 changes: 1 addition & 3 deletions packages/sails-hook-content/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ 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.config.content)
)
sails.config.shipwright.build.plugins.push(pluginSailsContent(sails))
}
}
}
Expand Down

0 comments on commit 8168c36

Please sign in to comment.