Skip to content

Commit

Permalink
Add YAML support
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
RobLoach committed Oct 2, 2020
1 parent ba34063 commit 9edf2f4
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 25 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v3.0.0: 2020-10-02

- YAML file support

## v2.2.1: 2020-04-04

- Update dependencies
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Greenkeeper badge](https://badges.greenkeeper.io/kalamuna/metalsmith-metadata-files.svg)](https://greenkeeper.io/)
[![Dependency Status](https://david-dm.org/kalamuna/metalsmith-metadata-files.png)](https://david-dm.org/kalamuna/metalsmith-metadata-files)

[Metalsmith](http://metalsmith.io) plugin to inject file metadata from matching `.json` files.
[Metalsmith](http://metalsmith.io) plugin to inject file metadata from matching `.json` or `.yaml` files.

## Installation

Expand All @@ -18,7 +18,7 @@ If you are using the command-line version of Metalsmith, you can install via npm
{
"plugins": {
"metalsmith-metadata-files": {
"pattern": "**.json"
"pattern": "{**.json,**.yaml}"
}
}
}
Expand All @@ -32,13 +32,13 @@ If you are using the JS Api for Metalsmith, then you can require the module and
var metadataFiles = require('metalsmith-metadata-files');

metalsmith.use(metadataFiles({
'pattern': '**.json'
'pattern': '{**.json,**.yaml}'
}));
```

## Convention

Create `.json` files along-side your content. The data from these files will be injected into the metadata into the matching file.
Create `.json` or `.yaml` files along-side your content. The data from these files will be injected into the metadata into the matching file.

### Example

Expand Down Expand Up @@ -112,7 +112,7 @@ String values that begin with `metadata-files://` will inject the file into the

### `.pattern`

The pattern used to find the JSON files. Defaults to `*.json`.
The pattern used to find the JSON files. Defaults to `{*.json|*.yaml}`.

### `.patternOptions`

Expand Down
45 changes: 28 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,42 @@ const path = require('path')
const extend = require('extend-shallow')
const multimatch = require('multimatch')
const traverse = require('traverse')
const yaml = require('yaml')

module.exports = function (opts) {
module.exports = function (options) {
// Prepare the options.
opts = opts || {}
opts.pattern = opts.pattern || '*.json'
opts.patternOptions = opts.patternOptions || {matchBase: true}
opts.inheritFilePrefix = opts.inheritFilePrefix || 'metadata-files://'
options = options || {}
options.pattern = options.pattern || '{*.json,*.yaml}'
options.patternOptions = options.patternOptions || {matchBase: true}
options.inheritFilePrefix = options.inheritFilePrefix || 'metadata-files://'

// Execute the plugin.
return function (files, metalsmith, done) {
// Find every JSON file.
const filesKeys = Object.keys(files)
const jsonFiles = multimatch(filesKeys, opts.pattern, opts.patternOptions)
const jsonFiles = multimatch(filesKeys, options.pattern, options.patternOptions)
let jsonFile = null
for (const i in jsonFiles) {
if (jsonFiles[i]) {
// Retrieve information about the JSON file.
jsonFile = jsonFiles[i]
let contents = {}

// Retrieve the JSON contents.
let contents = null
try {
contents = JSON.parse(files[jsonFile].contents)
} catch (error) {
return done(error + ' ' + jsonFile)
// Determine whether to parse it with JSON or YAML.
if (jsonFile.endsWith('.yaml')) {
// Pass the contents through YAML.
try {
contents = yaml.parse(files[jsonFile].contents.toString())
} catch (error) {
return done(error + ' (' + jsonFile + ')')
}
} else {
// Retrieve the JSON contents.
try {
contents = JSON.parse(files[jsonFile].contents)
} catch (error) {
return done(error + ' (' + jsonFile + ')')
}
}

files[jsonFile].metadata = contents
Expand All @@ -40,14 +51,14 @@ module.exports = function (opts) {
// See if the JSON file has metadata.
if (files[jsonFile].metadata) {
// Traverse through all entries in the metadata.
traverse(files[jsonFile].metadata).forEach(function (val) {
traverse(files[jsonFile].metadata).forEach(function (value) {
// Check if the object is a string.
if (typeof val === 'string' || val instanceof String) {
if (typeof value === 'string' || value instanceof String) {
// See if it starts with metadata-files://.
const inheritFileLength = opts.inheritFilePrefix.length
if (val.slice(0, inheritFileLength) === opts.inheritFilePrefix) {
const inheritFileLength = options.inheritFilePrefix.length
if (value.slice(0, inheritFileLength) === options.inheritFilePrefix) {
// Retrieve the metadata file that it is to retrieve.
const objectFile = val.slice(inheritFileLength)
const objectFile = value.slice(inheritFileLength)
// Find its own metadata.
if (files[objectFile] && files[objectFile].metadata) {
// Update the object to be the injected metadata.
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "metalsmith-metadata-files",
"version": "2.2.1",
"version": "3.0.0",
"description": "Metalsmith Metadata Files Plugin",
"files": [
"index.js"
Expand All @@ -26,11 +26,12 @@
"dependencies": {
"extend-shallow": "^3.0.2",
"multimatch": "^4.0.0",
"traverse": "^0.6.6"
"traverse": "^0.6.6",
"yaml": "^1.10.0"
},
"devDependencies": {
"assert-dir-equal": "1.*",
"jstransformer-twig": "^1.6.1",
"jstransformer-twig": "^1.6.2",
"metalsmith": "2.*",
"metalsmith-jstransformer": "*",
"rimraf": "^3.0.2",
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/yaml/expected/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="hello">Hello, World!</div>
1 change: 1 addition & 0 deletions test/fixtures/yaml/expected/subfolder/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="goodbye">Goodbye, Cruel World!</div>
1 change: 1 addition & 0 deletions test/fixtures/yaml/src/example.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="hello">Hello, {{name}}!</div>
1 change: 1 addition & 0 deletions test/fixtures/yaml/src/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: World
1 change: 1 addition & 0 deletions test/fixtures/yaml/src/subfolder/index.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="goodbye">Goodbye, {{name}}!</div>
1 change: 1 addition & 0 deletions test/fixtures/yaml/src/subfolder/index.html.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: "Cruel World"
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ testit('metalsmith-jstransformer', () => {
test('basic')
test('inherited')
test('ref')
test('yaml')
test('opts-inheritfileprefix', {
'..': {
inheritFilePrefix: '@metadatafile/'
Expand Down

0 comments on commit 9edf2f4

Please sign in to comment.