~askonomm/contentmachine

9a1f42d5d88ea631f5c97ee558c8acf4add4046e — Asko N├Ámm 4 months ago 69b1b5e master
Make Markdown parsing optional, add semicolons when needed and turn let into const.
3 files changed, 58 insertions(+), 43 deletions(-)

M README.md
M contentmachine.js
M package.json
M README.md => README.md +13 -5
@@ 54,7 54,7 @@ const contentmachine = require('contentmachine')
To retrieve all of the content in a directory, simply initialize ContentMachine with your provided directory and call `items()` on it, like this:

```Javascript
let content = new contentmachine({directory: "./Blog/"})
const content = new contentmachine({directory: "./Blog/"})

content.items().then(items => {



@@ 70,7 70,7 @@ This will return you an array of objects.
To retrieve a specific content item in a directory, initialize ContentMachine and call `item({key: "key", value: "value"})` on it (where key is corresponding with the YAML meta key and the value with its value in the file), like this:

```Javascript
let content = new contentmachine({directory: "./Blog/"})
const content = new contentmachine({directory: "./Blog/"})

content.item({key: "slug", value: "hello-world"}).then(item => {



@@ 84,7 84,7 @@ content.item({key: "slug", value: "hello-world"}).then(item => {
You can order content by any meta key in descending or ascending order. For example; if we want to order content by the meta key `date` in a descending order, we would do the following:

```Javascript
let content = new contentmachine({directory: "./Blog/", orderBy: "date", order: "desc"})
const content = new contentmachine({directory: "./Blog/", orderBy: "date", order: "desc"})
```

Likewise if you want it to be in ascending order, simply change `order` to `asc`.


@@ 94,7 94,15 @@ Likewise if you want it to be in ascending order, simply change `order` to `asc`
You can limit and offset content by adding `limit` and/or `offset` to the constructor. For example:

```Javascript
let content = new contentmachine({directory: "./Blog", limit: 100, offset: 50})
const content = new contentmachine({directory: "./Blog", limit: 100, offset: 50})
```

Note that if you don't specify limit or offset, then they fall back to their default values 10 and 0, respectively.
\ No newline at end of file
Note that if you don't specify limit or offset, then they fall back to their default values 10 and 0, respectively.

#### Turning off Markdown parsing

There might be a use case where you don't want the body of the document to be parsed into HTML from Markdown and you wish to get plaintext instead. In that case, simply pass `parseMarkdown: false` to the constructor of ContentMachine, like so:

```JavaScript
const content = new contentmachine({directory: "./Blog", parseMarkdown: false});
```
\ No newline at end of file

M contentmachine.js => contentmachine.js +44 -37
@@ 1,24 1,29 @@
const path = require('path')
const fs = require('fs')
const fm = require('front-matter')
const marked = require('marked')

class ContentMachine {

	directory = ''
	orderBy = false
	order = false
	limit = 10
	offset = 0
	directory = '';
	parseMarkdown = true;
	orderBy = false;
	order = false;
	limit = 10;
	offset = 0;

	constructor({directory = false, orderBy = false, order = false, limit = false, offset = false}) {
	constructor({directory = false, parseMarkdown, orderBy = false, order = false, limit = false, offset = false}) {

		if(directory) this.directory = directory
		if(orderBy) this.orderBy = orderBy
		if(order) this.order = order
		if(limit) this.limit = limit
		if(offset) this.offset = offset
    if(directory) this.directory = directory;
		if(orderBy) this.orderBy = orderBy;
		if(order) this.order = order;
		if(limit) this.limit = limit;
    if(offset) this.offset = offset;
    
    if(typeof parseMarkdown !== 'undefined') {

      this.parseMarkdown = parseMarkdown;

    }
	}

	/**


@@ 32,30 37,30 @@ class ContentMachine {

				if(err) {

					console.log('Unable to scan directory: ' + this.directory)
					resolve([])
					console.log('Unable to scan directory: ' + this.directory);
					resolve([]);

				}

				if(files) {

					var markdownFiles = []
					var fileCounter = files.length
					var markdownFiles = [];
					var fileCounter = files.length;

					files.forEach(file => {

						if(file.endsWith('.md')) {

							markdownFiles.push(file)
							markdownFiles.push(file);

						}

						fileCounter--
						fileCounter--;

						// All done?
						if(fileCounter == 0) {

							resolve(markdownFiles)
							resolve(markdownFiles);

						}



@@ 63,8 68,8 @@ class ContentMachine {

				} else {

					console.log('No files were found.')
					resolve([])
					console.log('No files were found.');
					resolve([]);

				}



@@ 83,8 88,8 @@ class ContentMachine {

			this._getContentFiles().then(files => {

				var items = []
				var fileCounter = files.length
				var items = [];
				var fileCounter = files.length;

				if(files.length) {



@@ 94,25 99,27 @@ class ContentMachine {

							if(err) {

								reject('Something went wrong trying to read the file: ' + file)
								reject('Something went wrong trying to read the file: ' + file);

							}

							// Parse data on file
							let parsedContent = fm(contents)

              console.log(this.parseMarkdown);

							// Add to items
							items.push({
								meta: parsedContent.attributes,
								content: marked(parsedContent.body)
								content: this.parseMarkdown ? marked(parsedContent.body) : parsedContent.body
							})

							fileCounter--
							fileCounter--;

							// All done?
							if(fileCounter == 0) {

								resolve(items)
								resolve(items);

							}



@@ 122,14 129,14 @@ class ContentMachine {

				} else {

					resolve([])
					resolve([]);

				}

			}).catch(err => {

				console.log('caught', err)
				resolve([])
				console.log('caught', err);
				resolve([]);

			})



@@ 142,7 149,7 @@ class ContentMachine {
	 */
	_sortItemsByKeyDesc(key, items) {

		return items.sort((a, b) => (a.meta[key] < b.meta[key] ? 1 : -1))
		return items.sort((a, b) => (a.meta[key] < b.meta[key] ? 1 : -1));

	}



@@ 151,7 158,7 @@ class ContentMachine {
	 */
	_sortItemsByKeyAsc(key, items) {

		return items.sort((a, b) => (a.meta[key] > b.meta[key] ? 1 : -1))
		return items.sort((a, b) => (a.meta[key] > b.meta[key] ? 1 : -1));

	}



@@ 169,17 176,17 @@ class ContentMachine {

					if(this.order === 'asc') {

						resolve(this._sortItemsByKeyAsc(this.orderBy, items).slice(this.offset, this.limit))
						resolve(this._sortItemsByKeyAsc(this.orderBy, items).slice(this.offset, this.limit));

					} else {

						resolve(this._sortItemsByKeyDesc(this.orderBy, items).slice(this.offset, this.limit))
						resolve(this._sortItemsByKeyDesc(this.orderBy, items).slice(this.offset, this.limit));

					}

				}).catch(err => {

					console.log('caught', err)
					console.log('caught', err);

				})



@@ 192,11 199,11 @@ class ContentMachine {

				this._parseContentFilesIntoItems().then(items => {

					resolve(items.slice(this.offset, this.limit))
					resolve(items.slice(this.offset, this.limit));

				}).catch(err => {

					console.log('caught', err)
					console.log('caught', err);

				})


M package.json => package.json +1 -1
@@ 1,6 1,6 @@
{
  "name": "contentmachine",
  "version": "1.0.0",
  "version": "1.1.0",
  "description": "A static content engine",
  "main": "contentmachine.js",
  "keywords": [