From 730f418a27f8aa1c625877cfc6fac38126e0a70f Mon Sep 17 00:00:00 2001 From: David Longworth Date: Fri, 31 Mar 2017 18:05:02 +0100 Subject: [PATCH] feat: meta array for markdown datasources addresses #139 --- dadi/lib/providers/markdown.js | 26 +++++++++++++++++++++----- workspace/partials/post.dust | 4 ++-- workspace/posts/assets.md | 1 - workspace/posts/image.md | 1 - workspace/posts/pages.md | 1 - workspace/posts/paginated.md | 1 - workspace/posts/site-name.md | 1 - workspace/posts/welcome.md | 1 - workspace/utils/helpers/markdown.js | 14 -------------- 9 files changed, 23 insertions(+), 27 deletions(-) delete mode 100644 workspace/utils/helpers/markdown.js diff --git a/dadi/lib/providers/markdown.js b/dadi/lib/providers/markdown.js index 67327dee..429b3fda 100644 --- a/dadi/lib/providers/markdown.js +++ b/dadi/lib/providers/markdown.js @@ -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' } /** @@ -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) @@ -122,7 +127,9 @@ 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) { @@ -130,14 +137,23 @@ MarkdownProvider.prototype.parseRawDataAsync = function parseRawDataAsync (data, 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 }) diff --git a/workspace/partials/post.dust b/workspace/partials/post.dust index 3b43104e..f33296e2 100644 --- a/workspace/partials/post.dust +++ b/workspace/partials/post.dust @@ -1,6 +1,6 @@
-

{attributes.title}

+

{attributes.title}

By {attributes.author} @@ -15,5 +15,5 @@

- {@markdown}{contentText|s}{/markdown} + {contentHtml|s}
\ No newline at end of file diff --git a/workspace/posts/assets.md b/workspace/posts/assets.md index dab5f739..a31dc1c3 100644 --- a/workspace/posts/assets.md +++ b/workspace/posts/assets.md @@ -1,7 +1,6 @@ --- date: 2016-02-17 title: How to serve static assets -handle: assets author: The DADI Team --- diff --git a/workspace/posts/image.md b/workspace/posts/image.md index 91293be3..cedf7ec8 100644 --- a/workspace/posts/image.md +++ b/workspace/posts/image.md @@ -1,7 +1,6 @@ --- date: 2015-04-17 title: Look, a post with an image in -handle: images author: The DADI Team --- diff --git a/workspace/posts/pages.md b/workspace/posts/pages.md index 0df09dd5..002afa1c 100644 --- a/workspace/posts/pages.md +++ b/workspace/posts/pages.md @@ -1,7 +1,6 @@ --- date: 2016-04-17 title: How to create a new page -handle: pages author: The DADI Team --- diff --git a/workspace/posts/paginated.md b/workspace/posts/paginated.md index 4ca10072..57b37c6c 100644 --- a/workspace/posts/paginated.md +++ b/workspace/posts/paginated.md @@ -1,7 +1,6 @@ --- date: 2014-04-17 title: We can handle pagination too... -handle: pagination author: The DADI Team --- diff --git a/workspace/posts/site-name.md b/workspace/posts/site-name.md index d4c9cca2..aec1c508 100644 --- a/workspace/posts/site-name.md +++ b/workspace/posts/site-name.md @@ -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 --- diff --git a/workspace/posts/welcome.md b/workspace/posts/welcome.md index 2193c14b..4c18caeb 100644 --- a/workspace/posts/welcome.md +++ b/workspace/posts/welcome.md @@ -1,7 +1,6 @@ --- date: 2016-05-10 title: Successfully installed and running! -handle: welcome author: The DADI Team --- diff --git a/workspace/utils/helpers/markdown.js b/workspace/utils/helpers/markdown.js deleted file mode 100644 index 4f8b7cec..00000000 --- a/workspace/utils/helpers/markdown.js +++ /dev/null @@ -1,14 +0,0 @@ -var dust = require('dustjs-linkedin') -var marked = require('marked') - -/* -* Returns the markdown content formatted as HTML -*/ -dust.helpers.markdown = function (chunk, context, bodies, params) { - if (bodies.block) { - return chunk.capture(bodies.block, context, function (string, chunk) { - chunk.end(marked(string)) - }) - } - return chunk -} \ No newline at end of file