Skip to content

Commit

Permalink
rss, atom, json feed support
Browse files Browse the repository at this point in the history
  • Loading branch information
stamat committed Sep 21, 2023
1 parent 2ad5a61 commit 9e7619a
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 22 deletions.
28 changes: 28 additions & 0 deletions changelog/feed.rss
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>💩 Poops/</title>
<description>Straightforward, no-bullshit bundler for the web./</description>
<link>https://stamat.github.io/poops</link>
<lastBuildDate>Thu, 21 Sep 2023 20:57:25 +0200</lastBuildDate>

<pubDate>Thu, 21 Sep 2023 15:42:00 +0200</pubDate>
<ttl>1800</ttl>


<item>
<title>Blog Functionality</title>
<link>https://stamat.github.io/poops/changelog/blog-functionality.html</link>
<description>Hey folks, this is my second blog post.</description>
<pubDate>Thu, 21 Sep 2023 15:42:00 +0200</pubDate>
</item>

<item>
<title>Added Front Matter support!</title>
<link>https://stamat.github.io/poops/changelog/front-matter.html</link>
<description>Front matter is an awesome idea! Since the static site generator functionality of Poops is inspired by Jekyll and the genius of peeps that came up with the idea of front matter, I decided to add it to Poops as well. Made things super simple! And that's what Poops is all about. Making things simple.</description>
<pubDate>Tue, 19 Sep 2023 02:00:00 +0200</pubDate>
</item>

</channel>
</rss>
21 changes: 21 additions & 0 deletions example/src/markup/changelog/feed.rss
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>{{ site.title }}/</title>
<description>{{ site.description }}/</description>
<link>{{ site.url }}</link>
<lastBuildDate>{{ '' | date('ddd, DD MMM YYYY HH:mm:ss ZZ') }}</lastBuildDate>
{% set lastPost = changelog.items[0] %}
<pubDate>{{ lastPost.date | date('ddd, DD MMM YYYY HH:mm:ss ZZ') }}</pubDate>
<ttl>1800</ttl>
{% set posts = changelog.items.slice(0, 10) %}
{% for post in posts %}
<item>
<title>{{ post.title }}</title>
<link>{{ site.url }}/{{ post.url }}</link>
<description>{{ post.description }}</description>
<pubDate>{{ post.date | date('ddd, DD MMM YYYY HH:mm:ss ZZ') }}</pubDate>
</item>
{% endfor %}
</channel>
</rss>
2 changes: 1 addition & 1 deletion example/src/markup/changelog/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h1>Changelog</h1>
{% for post in changelog.pageItems %}
<div class="post">
<h2><a href="{{ relativePathPrefix }}{{ post.url }}">{{ post.title }}</a></h2>
<div class="date">{{ post.date | format }}</div>
<div class="date">{{ post.date | date }}</div>
{{ post.description }}
</div>
{% endfor %}
Expand Down
42 changes: 30 additions & 12 deletions lib/markups.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ module.exports = class Markups {
return require('marked').parse(str)
})

this.nunjucksEnv.addFilter('format', (str, template) => {
this.nunjucksEnv.addFilter('date', (str, template) => {
if (!template) template = this.config.markup.options.timeDateFormat || this.config.markup.timeDateFormat
if (!template) return str
const date = new Date(str)
const date = !str || str.trim() === '' ? new Date() : new Date(str)
return require('moment')(date).format(template)
})

Expand Down Expand Up @@ -168,7 +168,7 @@ module.exports = class Markups {

markupDefaultExcludes = [...new Set(markupDefaultExcludes)] // Remove duplicates

return `!(${markupDefaultExcludes.join('|')})/**/*.+(html|njk|md)`
return `!(${markupDefaultExcludes.join('|')})/**/*.+(html|xml|rss|atom|json|njk|md)`
}

getSingleCollectionData(collectionName) {
Expand All @@ -195,9 +195,16 @@ module.exports = class Markups {
frontMatter.filePath = path.relative(process.cwd(), file)
frontMatter.collection = collectionName
frontMatter.url = path.join(collectionName, path.basename(frontMatter.filePath))
if (path.extname(frontMatter.filePath) === '.md') {
frontMatter.url = frontMatter.url.replace(/\.md$/, '.html')

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
}

if (!frontMatter.title) {
frontMatter.title = path.basename(frontMatter.filePath, path.extname(frontMatter.filePath))
}
Expand Down Expand Up @@ -323,7 +330,7 @@ module.exports = class Markups {
}

if (pathIsDirectory(markupIn)) {
const markupFiles = [...glob.sync(path.join(markupIn, this.generateMarkupGlobPattern(this.includePaths))), ...glob.sync(path.join(markupIn, '*.+(html|njk|md)'))]
const markupFiles = [...glob.sync(path.join(markupIn, this.generateMarkupGlobPattern(this.includePaths))), ...glob.sync(path.join(markupIn, '*.+(html|xml|rss|atom|json|njk|md)'))]
const collectionData = this.getCollectionData(this.config.markup.collections || this.config.markup.options.collections)
const compilePromises = []
markupFiles.forEach((file) => {
Expand All @@ -333,7 +340,7 @@ module.exports = class Markups {
// Create pagination for collection pages
if (relativePathParts.length > 1 &&
collectionData[relativePathParts[0]] &&
relativePathParts[1] === 'index.html' &&
(relativePathParts[1] === 'index.html' || relativePathParts[1] === 'index.njk') &&
collectionData[relativePathParts[0]].items.length > 0) {
let frontMatter = {}
const source = fs.readFileSync(file, 'utf-8')
Expand Down Expand Up @@ -408,11 +415,16 @@ module.exports = class Markups {
}

collectionData.relativePathPrefix = this.getRelativePathPrefix(markupOutDir)

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

fs.writeFileSync(markupOut, result)
})
compilePromises.push(compilePromise)
Expand All @@ -435,9 +447,15 @@ module.exports = class Markups {
additionalContext.relativePathPrefix = this.getRelativePathPrefix(markupOutDir)

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

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
10 changes: 2 additions & 8 deletions poops.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ async function poops() {
chokidar.watch(config.watch, { ignoreInitial: true }).on('change', (file) => {
if (/(\.m?js|\.ts)$/i.test(file)) scripts.compile()
if (/(\.sass|\.scss|\.css)$/i.test(file)) styles.compile()
if (/(\.html|\.njk|\.md)$/i.test(file)) markups.compile()
if (/(\.html|\.xml|\.rss|\.atom|\.njk|\.md)$/i.test(file)) markups.compile()

// TODO: We can actually reload the page only if the data file from data has changed.
if (/(\.json|\.ya?ml)$/i.test(file)) {
Expand All @@ -150,13 +150,7 @@ async function poops() {
})
}
}).on('unlink', (file) => {
if (/(\.html|\.njk|\.md)$/i.test(file)) markups.compile()

if (/(\.json|\.ya?ml)$/i.test(file)) {
markups.reloadDataFiles().then(() => {
markups.compile()
})
}
if (/(\.html|\.xml|\.rss|\.atom|\.njk|\.md)$/i.test(file)) markups.compile()
}).on('add', (file) => {
if (/(\.json|\.ya?ml)$/i.test(file)) {
markups.reloadDataFiles().then(() => {
Expand Down
3 changes: 2 additions & 1 deletion 💩.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"options": {
"site": {
"title": "💩 Poops",
"description": "Straightforward, no-bullshit bundler for the web."
"description": "Straightforward, no-bullshit bundler for the web.",
"url": "https://stamat.github.io/poops"
},
"data": [
"_data/features.yaml",
Expand Down

0 comments on commit 9e7619a

Please sign in to comment.