Closes #169. Add YAML Front Matter support to Markdown beautification
This commit is contained in:
parent
20206a654c
commit
e7a980045d
|
@ -1,3 +1,7 @@
|
||||||
|
---
|
||||||
|
{}
|
||||||
|
---
|
||||||
|
|
||||||
- item
|
- item
|
||||||
- item
|
- item
|
||||||
- item
|
- item
|
||||||
|
|
|
@ -1,7 +1,23 @@
|
||||||
---
|
---
|
||||||
title: This is a title!
|
title: "This is a title!"
|
||||||
|
name: Derek Worthen
|
||||||
|
age: young
|
||||||
|
contact: null
|
||||||
|
email: "email@domain.com"
|
||||||
|
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
|
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
---
|
|
||||||
title: This is a title!
|
|
||||||
---
|
|
||||||
|
|
||||||
stuff
|
|
||||||
|
|
||||||
more stuff
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
---
|
||||||
|
|
||||||
|
title: This is a title!
|
||||||
|
|
||||||
|
name: Derek Worthen
|
||||||
|
age: young
|
||||||
|
contact:
|
||||||
|
email: email@domain.com
|
||||||
|
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
|
|
@ -81,30 +81,33 @@ beautify = ({onSave}) ->
|
||||||
# Do nothing, is undefined
|
# Do nothing, is undefined
|
||||||
else if text instanceof Error
|
else if text instanceof Error
|
||||||
showError(text)
|
showError(text)
|
||||||
else if oldText isnt text
|
else if typeof text is "string"
|
||||||
# console.log "Replacing current editor's text with new text"
|
if oldText isnt text
|
||||||
posArray = getCursors(editor)
|
# console.log "Replacing current editor's text with new text"
|
||||||
# console.log "posArray: #{posArray}"
|
posArray = getCursors(editor)
|
||||||
origScrollTop = editor.getScrollTop()
|
# console.log "posArray: #{posArray}"
|
||||||
# console.log "origScrollTop: #{origScrollTop}"
|
origScrollTop = editor.getScrollTop()
|
||||||
if not forceEntireFile and isSelection
|
# console.log "origScrollTop: #{origScrollTop}"
|
||||||
selectedBufferRange = editor.getSelectedBufferRange()
|
if not forceEntireFile and isSelection
|
||||||
# console.log "selectedBufferRange: #{selectedBufferRange}"
|
selectedBufferRange = editor.getSelectedBufferRange()
|
||||||
editor.setTextInBufferRange selectedBufferRange, text
|
# console.log "selectedBufferRange: #{selectedBufferRange}"
|
||||||
else
|
editor.setTextInBufferRange selectedBufferRange, text
|
||||||
# console.log "setText"
|
else
|
||||||
editor.setText text
|
# console.log "setText"
|
||||||
# console.log "setCursors"
|
editor.setText text
|
||||||
setCursors editor, posArray
|
# console.log "setCursors"
|
||||||
# console.log "Done setCursors"
|
setCursors editor, posArray
|
||||||
# Let the scrollTop setting run after all the save related stuff is run,
|
# console.log "Done setCursors"
|
||||||
# otherwise setScrollTop is not working, probably because the cursor
|
# Let the scrollTop setting run after all the save related stuff is run,
|
||||||
# addition happens asynchronously
|
# otherwise setScrollTop is not working, probably because the cursor
|
||||||
setTimeout (->
|
# addition happens asynchronously
|
||||||
# console.log "setScrollTop"
|
setTimeout (->
|
||||||
editor.setScrollTop origScrollTop
|
# console.log "setScrollTop"
|
||||||
return
|
editor.setScrollTop origScrollTop
|
||||||
), 0
|
return
|
||||||
|
), 0
|
||||||
|
else
|
||||||
|
@showError(new Error("Unsupported beautification result '#{text}'."))
|
||||||
# else
|
# else
|
||||||
# console.log "Already Beautiful!"
|
# console.log "Already Beautiful!"
|
||||||
@loadingView.hide()
|
@loadingView.hide()
|
||||||
|
|
|
@ -23,12 +23,19 @@ module.exports = (getCmd, isStdout) ->
|
||||||
temp.cleanup()
|
temp.cleanup()
|
||||||
# Delete the output path
|
# Delete the output path
|
||||||
fs.unlink outputPath, (err) ->
|
fs.unlink outputPath, (err) ->
|
||||||
console.log "Deleting output file", err if err
|
# console.log "Deleting output file", err if err
|
||||||
return
|
return
|
||||||
return
|
return
|
||||||
|
|
||||||
# Process the command
|
# Process the command
|
||||||
processCmd = (cmd) ->
|
processCmd = (cmd, optCallback) ->
|
||||||
|
if optCallback? and typeof optCallback is "function"
|
||||||
|
# console.log('Optional Callback found')
|
||||||
|
cb = callback # Save callback for later
|
||||||
|
callback = (output) -> # Wrap callback (cb) with optCallback
|
||||||
|
# console.log('Callback called!', output)
|
||||||
|
optCallback(output, cb)
|
||||||
|
|
||||||
if typeof cmd is "string"
|
if typeof cmd is "string"
|
||||||
|
|
||||||
config = env: process.env
|
config = env: process.env
|
||||||
|
|
|
@ -1,18 +1,68 @@
|
||||||
###
|
###
|
||||||
Requires http: //johnmacfarlane.net/pandoc/
|
Requires http: //johnmacfarlane.net/pandoc/
|
||||||
###
|
###
|
||||||
getCmd = (inputPath, outputPath, options) ->
|
yamlFront = null
|
||||||
|
fs = null
|
||||||
|
yaml = null
|
||||||
|
allowUnsafeNewFunction = null
|
||||||
|
|
||||||
|
getCmd = (inputPath, outputPath, options, cb) ->
|
||||||
optionsStr = " --read markdown --write markdown --output \"" + outputPath + "\" \"" + inputPath + "\""
|
optionsStr = " --read markdown --write markdown --output \"" + outputPath + "\" \"" + inputPath + "\""
|
||||||
|
|
||||||
pandocPath = options.markdown_beautifier_path # jshint ignore: line
|
pandocPath = options.markdown_beautifier_path # jshint ignore: line
|
||||||
if pandocPath
|
yamlFrontMatter = options.yaml_front_matter # jshint ignore: line
|
||||||
|
cmd = ""
|
||||||
|
if pandocPath?
|
||||||
# Use absolute path
|
# Use absolute path
|
||||||
pandocPath + optionsStr
|
cmd = pandocPath + optionsStr
|
||||||
else
|
else
|
||||||
|
|
||||||
# Use command available in $PATH
|
# 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"
|
"use strict"
|
||||||
cliBeautify = require("./cli-beautify")
|
cliBeautify = require("./cli-beautify")
|
||||||
module.exports = cliBeautify(getCmd)
|
module.exports = cliBeautify(getCmd)
|
||||||
|
|
|
@ -98,6 +98,7 @@ module.exports =
|
||||||
|
|
||||||
# Markdown
|
# Markdown
|
||||||
markdown_pandoc_path: ""
|
markdown_pandoc_path: ""
|
||||||
|
markdown_yaml_front_matter: true
|
||||||
|
|
||||||
# Perl
|
# Perl
|
||||||
perl_perltidy_path: "perltidy"
|
perl_perltidy_path: "perltidy"
|
||||||
|
|
63
package.json
63
package.json
|
@ -40,6 +40,38 @@
|
||||||
"url": "https://github.com/vadirn"
|
"url": "https://github.com/vadirn"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"engines": {
|
||||||
|
"atom": ">0.50.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"analytics-node": "^1.0.2",
|
||||||
|
"async": "^0.9.0",
|
||||||
|
"atom-message-panel": "^1.1.1",
|
||||||
|
"coffee-formatter": "^0.1.1",
|
||||||
|
"editorconfig": "^0.11.4",
|
||||||
|
"emissary": "^1.0.0",
|
||||||
|
"extend": "^1.2.1",
|
||||||
|
"js-beautify": "^1.5.4",
|
||||||
|
"js-yaml": "^3.0.2",
|
||||||
|
"lodash": "2.4.1",
|
||||||
|
"loophole": "^1.0.0",
|
||||||
|
"node-dir": "^0.1.6",
|
||||||
|
"node-uuid": "^1.4.1",
|
||||||
|
"prettydiff": "^1.6.13",
|
||||||
|
"space-pen": "^4.3.0",
|
||||||
|
"strip-json-comments": "^0.1.3",
|
||||||
|
"temp": "^0.8.0",
|
||||||
|
"typescript-formatter": "~0.1.4",
|
||||||
|
"yaml-front-matter": "^3.2.3"
|
||||||
|
},
|
||||||
|
"activationEvents": [
|
||||||
|
"beautify",
|
||||||
|
"beautify:beautify-editor",
|
||||||
|
"beautify:beautify-file",
|
||||||
|
"beautify:beautify-directory",
|
||||||
|
"core:save",
|
||||||
|
"core:save-as"
|
||||||
|
],
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"atom",
|
"atom",
|
||||||
"beautify",
|
"beautify",
|
||||||
|
@ -78,36 +110,5 @@
|
||||||
"d",
|
"d",
|
||||||
"erb",
|
"erb",
|
||||||
"editorconfig"
|
"editorconfig"
|
||||||
],
|
|
||||||
"engines": {
|
|
||||||
"atom": ">0.50.0"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"analytics-node": "^1.0.2",
|
|
||||||
"async": "^0.9.0",
|
|
||||||
"atom-message-panel": "^1.1.1",
|
|
||||||
"coffee-formatter": "^0.1.1",
|
|
||||||
"editorconfig": "^0.11.4",
|
|
||||||
"emissary": "^1.0.0",
|
|
||||||
"extend": "^1.2.1",
|
|
||||||
"js-beautify": "^1.5.4",
|
|
||||||
"js-yaml": "^3.0.2",
|
|
||||||
"lodash": "2.4.1",
|
|
||||||
"loophole": "^1.0.0",
|
|
||||||
"node-dir": "^0.1.6",
|
|
||||||
"node-uuid": "^1.4.1",
|
|
||||||
"prettydiff": "^1.6.13",
|
|
||||||
"space-pen": "^4.3.0",
|
|
||||||
"strip-json-comments": "^0.1.3",
|
|
||||||
"temp": "^0.8.0",
|
|
||||||
"typescript-formatter": "~0.1.4"
|
|
||||||
},
|
|
||||||
"activationEvents": [
|
|
||||||
"beautify",
|
|
||||||
"beautify:beautify-editor",
|
|
||||||
"beautify:beautify-file",
|
|
||||||
"beautify:beautify-directory",
|
|
||||||
"core:save",
|
|
||||||
"core:save-as"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue