Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] fix shipwright undefined in template #8

Merged
merged 7 commits into from
Jul 24, 2024
Merged
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
Loading