Skip to content

Commit

Permalink
feat: meta array for markdown datasources
Browse files Browse the repository at this point in the history
addresses #139
  • Loading branch information
David Longworth committed Mar 31, 2017
1 parent f99dda2 commit 730f418
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 27 deletions.
26 changes: 21 additions & 5 deletions dadi/lib/providers/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const MarkdownProvider = function () {}
MarkdownProvider.prototype.initialise = function initialise (datasource, schema) {
this.datasource = datasource
this.schema = schema
this.extension = schema.datasource.source.extension ? schema.datasource.source.extension : 'md'
}

/**
Expand Down Expand Up @@ -52,9 +53,13 @@ MarkdownProvider.prototype.processSortParameter = function processSortParameter
MarkdownProvider.prototype.load = function load (requestUrl, done) {
try {
const sourcePath = path.normalize(this.schema.datasource.source.path)
const filenames = fs.readdirSync(sourcePath)
const filepaths = filenames.map(i => path.join(sourcePath, i)).filter(item => !/(^|\/)\.[^/.]/g.test(item))

// Only get files that match the extension provided, or the default set above
const extFilter = new RegExp('.' + this.extension + '$', 'g')
const filenames = fs.readdirSync(sourcePath).filter(i => extFilter.test(i))
const filepaths = filenames.map(i => path.join(sourcePath, i))

// Process each file
async.map(filepaths, this.readFileAsync, (err, readResults) => {
if (err) return done(err, null)

Expand Down Expand Up @@ -122,22 +127,33 @@ MarkdownProvider.prototype.load = function load (requestUrl, done) {
}

MarkdownProvider.prototype.readFileAsync = function readFileAsync (filename, callback) {
fs.readFile(filename, 'utf8', callback)
fs.readFile(filename, 'utf8', function(err, data){
return callback(err, {_name: filename, _contents: data})
})
}

MarkdownProvider.prototype.parseRawDataAsync = function parseRawDataAsync (data, callback) {
const yamlRegex = /---[\n\r]+([\s\S]*)[\n\r]+---[\n\r]+([\s\S]*)/
const posts = []

for (let i = 0; i < data.length; i++) {
const bits = yamlRegex.exec(data[i])
const bits = yamlRegex.exec(data[i]._contents)
const attributes = yaml.safeLoad(bits[1] || '')
const contentText = bits[2] || ''
const contentHtml = marked(contentText)
const parsedPath = path.parse(data[i]._name)

// Some info about the file
const meta = {
location: data[i]._name,
extension: parsedPath.ext,
handle: parsedPath.name
}

posts.push({
original: data[i],
meta,
attributes,
original: data[i]._contents,
contentText,
contentHtml
})
Expand Down
4 changes: 2 additions & 2 deletions workspace/partials/post.dust
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<article>
<header>
<h2><a href="/post/{attributes.handle}">{attributes.title}</a></h2>
<h2><a href="/post/{meta.handle}">{attributes.title}</a></h2>
<p>
By <strong>{attributes.author}</strong>

Expand All @@ -15,5 +15,5 @@
</p>
</header>

{@markdown}{contentText|s}{/markdown}
{contentHtml|s}
</article>
1 change: 0 additions & 1 deletion workspace/posts/assets.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
date: 2016-02-17
title: How to serve static assets
handle: assets
author: The DADI Team
---

Expand Down
1 change: 0 additions & 1 deletion workspace/posts/image.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
date: 2015-04-17
title: Look, a post with an image in
handle: images
author: The DADI Team
---

Expand Down
1 change: 0 additions & 1 deletion workspace/posts/pages.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
date: 2016-04-17
title: How to create a new page
handle: pages
author: The DADI Team
---

Expand Down
1 change: 0 additions & 1 deletion workspace/posts/paginated.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
date: 2014-04-17
title: We can handle pagination too...
handle: pagination
author: The DADI Team
---

Expand Down
1 change: 0 additions & 1 deletion workspace/posts/site-name.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
date: 2016-04-05
title: How to change the site name in the header
handle: site-name
author: The DADI Team
---

Expand Down
1 change: 0 additions & 1 deletion workspace/posts/welcome.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
date: 2016-05-10
title: Successfully installed and running!
handle: welcome
author: The DADI Team
---

Expand Down
14 changes: 0 additions & 14 deletions workspace/utils/helpers/markdown.js

This file was deleted.

0 comments on commit 730f418

Please sign in to comment.