Skip to content

Commit

Permalink
page.url for current page discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
stamat committed Sep 22, 2023
1 parent ebef7ce commit 93928f5
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions lib/markups.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,7 @@ module.exports = class Markups {
frontMatter.collection = collectionName
frontMatter.url = path.join(collectionName, path.basename(frontMatter.filePath))

switch (path.extname(frontMatter.filePath)) {
case '.md':
frontMatter.url = frontMatter.url.replace(/\.md$/, '.html')
break
case '.njk':
frontMatter.url = frontMatter.url.replace(/\.njk$/, '.html')
break
}
frontMatter.url = this.replaceOutExtensions(frontMatter.filePath)

if (!frontMatter.title) {
frontMatter.title = path.basename(frontMatter.filePath, path.extname(frontMatter.filePath))
Expand Down Expand Up @@ -290,11 +283,35 @@ module.exports = class Markups {
return relativePathPrefix
}

getPageUrl(outputPath) {
outputPath = this.replaceOutExtensions(outputPath)
return /index\.[a-z]+$/.test(path.basename(outputPath)) ? path.relative(process.cwd(), path.dirname(outputPath)) : path.relative(process.cwd(), outputPath)
}

replaceOutExtensions(outputPath) {
switch (path.extname(outputPath)) {
case '.md':
outputPath = outputPath.replace(/\.md$/, '.html')
break
case '.njk':
outputPath = outputPath.replace(/\.njk$/, '.html')
break
}

return outputPath
}

compileEntry(templateName, additionalContext) {
const source = fs.readFileSync(templateName, 'utf-8')
const match = source.match(/^\s*---\s*([\s\S]*?)---\s*/) // Front matter match
const context = { page: {} }
let pageUrl

if (additionalContext) {
if (additionalContext._url) {
pageUrl = additionalContext._url
delete additionalContext._url
}
Object.assign(context, additionalContext)
}
if (match) {
Expand All @@ -306,6 +323,8 @@ module.exports = class Markups {
}
}

if (pageUrl) context.page.url = pageUrl

const env = this.nunjucksEnv
return new Promise((resolve, reject) => {
env.getTemplate(templateName).render(context, (error, result) => {
Expand Down Expand Up @@ -398,6 +417,7 @@ module.exports = class Markups {
}

collectionData.relativePathPrefix = this.getRelativePathPrefix(markupOutDir)
collectionData._url = this.getPageUrl(markupOut)

const compilePromise = this.compileEntry(file, collectionData).then((result) => {
fs.writeFileSync(markupOut, result)
Expand All @@ -417,16 +437,10 @@ module.exports = class Markups {
}

collectionData.relativePathPrefix = this.getRelativePathPrefix(markupOutDir)
const compilePromise = this.compileEntry(file, collectionData).then((result) => {
switch (path.extname(markupOut)) {
case '.md':
markupOut = markupOut.replace(/\.md$/, '.html')
break
case '.njk':
markupOut = markupOut.replace(/\.njk$/, '.html')
break
}
collectionData._url = this.getPageUrl(markupOut)

const compilePromise = this.compileEntry(file, collectionData).then((result) => {
markupOut = this.replaceOutExtensions(markupOut)
fs.writeFileSync(markupOut, result)
})
compilePromises.push(compilePromise)
Expand All @@ -447,17 +461,10 @@ module.exports = class Markups {
const markupOutDir = path.dirname(markupOut)
const additionalContext = {}
additionalContext.relativePathPrefix = this.getRelativePathPrefix(markupOutDir)
additionalContext._url = this.getPageUrl(markupOut)

return this.compileEntry(markupIn, additionalContext).then((result) => {
switch (path.extname(markupOut)) {
case '.md':
markupOut = markupOut.replace(/\.md$/, '.html')
break
case '.njk':
markupOut = markupOut.replace(/\.njk$/, '.html')
break
}

markupOut = this.replaceOutExtensions(markupOut)
fs.writeFileSync(markupOut, result)
console.log(`${pstyle.cyanBright + pstyle.bold}[markup]${pstyle.reset} ${pstyle.dim}Compiled:${pstyle.reset} ${pstyle.italic + pstyle.underline}${path.relative(process.cwd(), path.join(process.cwd(), this.config.markup.out, path.basename(markupIn)))}${pstyle.reset}`)
}).catch((err) => {
Expand Down

0 comments on commit 93928f5

Please sign in to comment.