-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #169. Add YAML Front Matter support to Markdown beautification
- Loading branch information
Showing
9 changed files
with
176 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
--- | ||
{} | ||
--- | ||
|
||
- item | ||
- item | ||
- item | ||
|
22 changes: 19 additions & 3 deletions
22
examples/simple-jsbeautifyrc/markdown/expected/yaml-front-matter.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,23 @@ | ||
--- | ||
title: This is a title! | ||
title: "This is a title!" | ||
name: Derek Worthen | ||
age: young | ||
contact: null | ||
email: "[email protected]" | ||
address: some location | ||
pets: | ||
- cat | ||
- dog | ||
- bat | ||
match: !<tag:yaml.org,2002:js/regexp> /pattern/gmi | ||
run: !<tag:yaml.org,2002:js/function> "function () {\n \n \n \n\n\n}" | ||
--- | ||
|
||
stuff | ||
- item | ||
- item | ||
- item | ||
|
||
1. one | ||
2. two | ||
3. three | ||
|
||
more stuff |
7 changes: 0 additions & 7 deletions
7
examples/simple-jsbeautifyrc/markdown/original/_yaml-front-matter.md
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
examples/simple-jsbeautifyrc/markdown/original/yaml-front-matter.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
|
||
title: This is a title! | ||
|
||
name: Derek Worthen | ||
age: young | ||
contact: | ||
email: [email protected] | ||
address: some location | ||
pets: | ||
- cat | ||
- dog | ||
- bat | ||
match: !!js/regexp /pattern/gim | ||
run: !!js/function function() { } | ||
|
||
|
||
|
||
--- | ||
|
||
- item | ||
- item | ||
- item | ||
|
||
1. one | ||
2. two | ||
2. three |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,68 @@ | ||
### | ||
Requires http: //johnmacfarlane.net/pandoc/ | ||
### | ||
getCmd = (inputPath, outputPath, options) -> | ||
optionsStr = " --read markdown --write markdown --output \"" + outputPath + "\" \"" + inputPath + "\"" | ||
yamlFront = null | ||
fs = null | ||
yaml = null | ||
allowUnsafeNewFunction = null | ||
|
||
getCmd = (inputPath, outputPath, options, cb) -> | ||
optionsStr = " --read markdown --write markdown --output \"" + outputPath + "\" \"" + inputPath + "\"" | ||
pandocPath = options.markdown_beautifier_path # jshint ignore: line | ||
if pandocPath | ||
|
||
yamlFrontMatter = options.yaml_front_matter # jshint ignore: line | ||
cmd = "" | ||
if pandocPath? | ||
# Use absolute path | ||
pandocPath + optionsStr | ||
cmd = pandocPath + optionsStr | ||
else | ||
|
||
# Use command available in $PATH | ||
"pandoc" + optionsStr | ||
cmd = "pandoc" + optionsStr | ||
|
||
if yamlFrontMatter? | ||
# console.log("YAML Front Matter!") | ||
fs ?= require "fs" | ||
fs.readFile(inputPath, (err, contents) -> | ||
# console.log('readFile', err, contents) | ||
return cb(err) if err | ||
|
||
# Parse with YAML front Matter | ||
yamlFront ?= require "yaml-front-matter" | ||
# console.log('Parse YAML Front Matter') | ||
allowUnsafeNewFunction ?= require("loophole").allowUnsafeNewFunction | ||
results = null | ||
try | ||
allowUnsafeNewFunction -> | ||
results = yamlFront.loadFront(contents) | ||
catch e | ||
return cb(e) | ||
newContents = results.__content # jshint ignore: line | ||
delete results.__content # jshint ignore: line | ||
# console.log('newContents', newContents) | ||
# Write out new contents to input file | ||
fs.writeFile(inputPath, newContents, (err) -> | ||
# console.log('writeFile', err) | ||
return cb(err) if err | ||
|
||
# Completetion callback to combine YAML Front Matter and Markdown | ||
completionCallback = (output, callback) -> | ||
# console.log('Completion callback!') | ||
try | ||
yaml ?= require "js-yaml" | ||
# Pre-pend YAML Front Matter to top of Markdown output | ||
front = yaml.dump(results) | ||
output = "---\n#{front}---\n\n#{output}" | ||
# console.log('final output!', output) | ||
return callback(output) | ||
catch e | ||
return callback(e) | ||
# Run it all together now! | ||
# console.log('Run!') | ||
return cb(cmd, completionCallback) | ||
) | ||
) | ||
return # Use Callback | ||
else | ||
return cmd # Return cmd synchronously | ||
"use strict" | ||
cliBeautify = require("./cli-beautify") | ||
module.exports = cliBeautify(getCmd) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters