Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
[*.js]
# All files unless overridden: UTF-8, Unix style line endings and
# final newline, no tabs, 2-space indentation.
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

# Trailing whitespace is significant in Markdown
[*.md]
trim_trailing_whitespace = false

# Makefiles must be indented with tabs
[Makefile]
indent_size = 8
indent_style = tab
14 changes: 14 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
extends:
- eslint:recommended
- plugin:prettier/recommended

env:
browser: false
node: true
mocha: true

rules:
# Allow an escape hatch for unused arguments.
no-unused-vars:
- error
- argsIgnorePattern: '^_(ignored|unused)(_[a-z0-9]+)?'
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
package-lock.json
test/fixtures/*/build/*
.DS_Store
.DS_Store
.eslintcache
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#! /bin/sh
. "${0%/*}/_/husky.sh"

npx lint-staged
5 changes: 5 additions & 0 deletions .lintstagedrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# notes:
# - for JS files, eslint runs prettier
# - I don't like what prettier does to Markdown
'*.js': 'eslint --cache --fix'
'*.{json,yml,yaml}': 'prettier --loglevel warn --write'
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.eslintcache
package-lock.json
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"endOfLine": "lf",
"tabWidth": 2,
"useTabs": false,
"trailingComma": "es5",
"printWidth": 80,
"singleQuote": true
}
50 changes: 26 additions & 24 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

var basename = require('path').basename;
var debug = require('debug')('metalsmith-markdown');
var dirname = require('path').dirname;
var extname = require('path').extname;
var join = require('path').join;
var markdownIt = require('markdown-it');
var minimatch = require('minimatch');
var micromatch = require('micromatch');

/**
* Expose `plugin`.
Expand All @@ -18,45 +17,48 @@ module.exports = plugin;
*
* @param {Object} options (optional) - options to pass to markdownIt
* @param {Object} options.plugin (optional) - options used by the plugin. Will not be passed to MarkdownIt
* @param {string} options.plugin.pattern - glob pattern for filtering which files to process (passed to minimatch.filter)
* @param {string} options.plugin.pattern - glob pattern for filtering which files to process (passed to micromatch())
* @param {string|Array} options.plugin.fields - field or list of fields which MarkdownIt should process
* @param {string} options.plugin.extension - file extension for output files
* @return {Function}
*/

function plugin(preset, options){
function plugin(preset, options) {
var defaults = {
pattern: '**/*.@(md|markdown)',
fields: 'contents',
extension: 'html'
}
extension: 'html',
};
var pluginOptions = defaults;

// handle cases where a preset isn't specified
var pluginOpts = false;
if (options && options.plugin) {
pluginOpts = options.plugin;
delete options.plugin;
} else if (!options && typeof preset === 'object' && preset.plugin){
} else if (!options && typeof preset === 'object' && preset.plugin) {
pluginOpts = preset.plugin;
delete preset.plugin;
}

if (pluginOpts) {
// merge defaults with supplied options
for (var prop in pluginOpts) {
for (var prop in pluginOpts) {
pluginOptions[prop] = pluginOpts[prop];
}
}
// normalize pluginOptions.fields into an array
if (typeof pluginOptions.fields === 'string') pluginOptions.fields = [pluginOptions.fields]
if (typeof pluginOptions.fields === 'string')
pluginOptions.fields = [pluginOptions.fields];

var markdown = new markdownIt(preset, options),
envSetter = function(){};
envSetter = function () {};

var plugin = function(files, metalsmith, done){
var plugin = function (files, metalsmith, done) {
setImmediate(done);
Object.keys(files).filter(minimatch.filter(pluginOptions.pattern)).forEach(function(file){
micromatch(Object.keys(files), pluginOptions.pattern).forEach(function (
file
) {
var data = files[file];
var dir = dirname(file);
var html = basename(file, extname(file)) + '.' + pluginOptions.extension;
Expand All @@ -68,13 +70,13 @@ function plugin(preset, options){
}

debug('converting file: %s', file);
pluginOptions.fields.forEach(function(field){
pluginOptions.fields.forEach(function (field) {
debug('- checking field: %s', field);
if (!data[field]) return
if (!data[field]) return;
debug('- converting field: %s', field);
var str = markdown.render(data[field].toString(), env);
data[field] = new Buffer(str);
})
data[field] = Buffer.from(str);
});

delete files[file];
files[html] = data;
Expand All @@ -85,23 +87,23 @@ function plugin(preset, options){

/* proxy parser methods to return plugin for inline use */

['use', 'set', 'enable', 'disable'].forEach(function(fn){
plugin[fn] = function(){
['use', 'set', 'enable', 'disable'].forEach(function (fn) {
plugin[fn] = function () {
var args = Array.prototype.slice.call(arguments);
markdown[fn].apply(markdown, args);
return plugin;
}
};
});

plugin.env = function(setter){
plugin.env = function (setter) {
envSetter = setter;
return plugin;
}
};

plugin.withParser = function(fn){
plugin.withParser = function (fn) {
fn(markdown);
return plugin;
}
};

return plugin;
}
}
Loading