~askonomm/contentmachine

0d1952fa3f600c47b02a5146785b916b042920bf — Asko N├Ámm 4 months ago 4624331
Only tabs, please.
1 files changed, 134 insertions(+), 134 deletions(-)

M contentmachine.js
M contentmachine.js => contentmachine.js +134 -134
@@ 5,237 5,237 @@ const marked = require('marked')

class ContentMachine {

  directory = ''
  orderBy = false
  order = false
  limit = 10
  offset = 0
	directory = ''
	orderBy = false
	order = false
	limit = 10
	offset = 0

  constructor({directory = false, orderBy = false, order = false, limit = false, offset = false}) {
	constructor({directory = false, 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

  }
	}

  /**
   * Retrieve content files from a specified directory
   */
  _getContentFiles() {
	/**
	 * Retrieve content files from a specified directory
	 */
	_getContentFiles() {

    return new Promise((resolve, reject) => {
		return new Promise((resolve, reject) => {

      fs.readdir(this.directory, (err, files) => {
			fs.readdir(this.directory, (err, files) => {

        if(err) {
				if(err) {

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

        }
				}

        if(files) {
				if(files) {

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

          files.forEach(file => {
					files.forEach(file => {

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

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

            }
						}

            fileCounter--
						fileCounter--

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

              resolve(markdownFiles)
							resolve(markdownFiles)

            }
						}

          })
					})

        } else {
				} else {

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

        }
				}

      })
			})

    })
		})

  }
	}

  /**
   * Parse content files
   */
  _parseContentFilesIntoItems() {
	/**
	 * Parse content files
	 */
	_parseContentFilesIntoItems() {

    return new Promise((resolve, reject) => {
		return new Promise((resolve, reject) => {

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

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

        if(files.length) {
				if(files.length) {

          files.forEach(file => {
					files.forEach(file => {

            fs.readFile(this.directory + '/' + file, 'utf8', (err, contents) => {
						fs.readFile(this.directory + '/' + file, 'utf8', (err, contents) => {

              if(err) {
							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)
							// Parse data on file
							let parsedContent = fm(contents)

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

              fileCounter--
							fileCounter--

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

                resolve(items)
								resolve(items)

              }
							}

            })
						})

          })
					})

        } else {
				} else {

          resolve([])
					resolve([])

        }
				}

      }).catch(err => {
			}).catch(err => {

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

      })
			})

    })
		})

  }
	}

  /**
   * Sort items by meta key, in descending order
   */
  _sortItemsByKeyDesc(key, items) {
	/**
	 * Sort items by meta key, in descending order
	 */
	_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))

  }
	}

  /**
   * Sort items by meta key, in ascending order
   */
  _sortItemsByKeyAsc(key, items) {
	/**
	 * Sort items by meta key, in ascending order
	 */
	_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))

  }
	}

  /**
   * Get content items
   */
  items() {
	/**
	 * Get content items
	 */
	items() {

    // Are we ordering?
    if(this.order && this.orderBy) {
		// Are we ordering?
		if(this.order && this.orderBy) {

      return new Promise((resolve, reject) => {
			return new Promise((resolve, reject) => {

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

          if(this.order === 'asc') {
					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 {
					} 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 => {
				}).catch(err => {

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

        })
				})

      })
			})

    // Otherwise return the items as they came
    } else {
		// Otherwise return the items as they came
		} else {

      return new Promise((resolve, reject) => {
			return new Promise((resolve, reject) => {

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

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

        }).catch(err => {
				}).catch(err => {

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

        })
				})

      })
			})

    }
		}

  }
	}

  /**
   * Get content item
   */
  item({key = false, value = false}) {
	/**
	 * Get content item
	 */
	item({key = false, value = false}) {

    return new Promise((resolve, reject) => {
		return new Promise((resolve, reject) => {

      if(key && value) {
			if(key && value) {

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

					resolve(items.filter(item => item.meta[key] === value)[0]);

        }).catch(err => {
				}).catch(err => {

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

        })
				})

      } else {
			} else {

        reject('No key or value present.')
				reject('No key or value present.')

      }
			}

    })
		})

  }
	}

}
	}

module.exports = ContentMachine