Skip to content

Commit

Permalink
[feat] add global content config (#3)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
DominusKelvin authored Jan 16, 2024
1 parent 95e6022 commit 64ff9ea
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
17 changes: 11 additions & 6 deletions packages/plugin-sails-content/lib/generate-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
)
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(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)
})
}
}
Expand Down
6 changes: 4 additions & 2 deletions packages/plugin-sails-content/lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand Down
2 changes: 1 addition & 1 deletion packages/sails-hook-content/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}
}
Expand Down

0 comments on commit 64ff9ea

Please sign in to comment.