[WIP] beautify code blocks in markdown
This commit is contained in:
parent
8722279281
commit
94fb44b2f9
|
@ -171,6 +171,7 @@
|
|||
"event-kit": "^2.3.0",
|
||||
"expand-home-dir": "0.0.3",
|
||||
"extend": "^3.0.1",
|
||||
"gfm-code-blocks": "^1.0.0",
|
||||
"gherkin": "^2.12.2",
|
||||
"handlebars": "^4.0.10",
|
||||
"js-beautify": "^1.6.14",
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Beautifiers = require("./beautifiers")
|
||||
module.exports = new Beautifiers()
|
|
@ -0,0 +1,52 @@
|
|||
module.exports = (text, logger) ->
|
||||
return new @Promise((resolve, reject) ->
|
||||
extractCodeBlocks = require 'gfm-code-blocks'
|
||||
beautifier = require '../../beautifier'
|
||||
cleanMarkdown = text
|
||||
codeBlocks = extractCodeBlocks(cleanMarkdown)
|
||||
beautifyBlockPromises = []
|
||||
for codeBlock in codeBlocks
|
||||
codeLanguage = codeBlock.lang.match(/^[\w\d]+/)?[0]
|
||||
logger.verbose "Beautify code block #{codeLanguage}"
|
||||
if !codeLanguage
|
||||
beautifyBlockPromises.push(null)
|
||||
else
|
||||
beautifyBlockPromises.push(new @Promise((resolve, reject) ->
|
||||
linesInBlock = codeBlock.block.split('\n')
|
||||
codePrefix = linesInBlock[0]
|
||||
codeSuffix = linesInBlock[linesInBlock.length - 1]
|
||||
code = linesInBlock.slice(1, linesInBlock.length - 1).join('\n')
|
||||
filePath = 'fakepath.' + codeLanguage
|
||||
grammar = atom.grammars.selectGrammar(filePath, code)
|
||||
grammarName = grammar.name
|
||||
allOptions = beautifier.getOptionsForPath(filePath)
|
||||
originalStart = codeBlock.start
|
||||
originalEnd = codeBlock.end
|
||||
if grammarName == 'Null Grammar'
|
||||
resolve(null)
|
||||
else
|
||||
beautifier.beautify(code, allOptions, grammarName, filePath)
|
||||
.then((cleanCode) =>
|
||||
if cleanCode
|
||||
resolve({
|
||||
block: [codePrefix, cleanCode, codeSuffix].join('\n')
|
||||
originalStart: originalStart
|
||||
originalEnd: originalEnd
|
||||
})
|
||||
else
|
||||
resolve(null)
|
||||
)
|
||||
.catch((e) =>
|
||||
logger.verbose "error while beautifying #{code.substring(0, 200)}...", e
|
||||
resolve(null)
|
||||
)
|
||||
))
|
||||
Promise.all(beautifyBlockPromises).then((cleanCodeBlocks) =>
|
||||
cleanCodeBlocks.reverse((b) -> !!b).forEach((codeBlock) =>
|
||||
if (codeBlock)
|
||||
cleanMarkdown = cleanMarkdown.substring(0, codeBlock.originalStart) + codeBlock.block + cleanMarkdown.substring(codeBlock.originalEnd)
|
||||
)
|
||||
).then(->
|
||||
resolve(cleanMarkdown)
|
||||
)
|
||||
)
|
|
@ -1,5 +1,6 @@
|
|||
"use strict"
|
||||
Beautifier = require('./beautifier')
|
||||
beautifyCodeBlocks = require './markdown-blocks'
|
||||
|
||||
module.exports = class Remark extends Beautifier
|
||||
name: "Remark"
|
||||
|
@ -28,16 +29,23 @@ module.exports = class Remark extends Beautifier
|
|||
strong: true
|
||||
emphasis: true
|
||||
position: true
|
||||
beautifyCodeBlocks: true
|
||||
}
|
||||
Markdown: true
|
||||
}
|
||||
|
||||
beautify: (text, language, options) ->
|
||||
logger = @logger;
|
||||
return new @Promise((resolve, reject) ->
|
||||
try
|
||||
remark = require 'remark'
|
||||
cleanMarkdown = remark().process(text, options).toString()
|
||||
resolve cleanMarkdown
|
||||
if options.beautifyCodeBlocks
|
||||
beautifyCodeBlocks(cleanMarkdown, logger)
|
||||
.then((t) => resolve(t))
|
||||
.catch((e) => reject(e))
|
||||
else
|
||||
resolve cleanMarkdown
|
||||
catch err
|
||||
@error("Remark error: #{err}")
|
||||
reject(err)
|
||||
|
|
|
@ -1,16 +1,26 @@
|
|||
"use strict"
|
||||
Beautifier = require('./beautifier')
|
||||
beautifyCodeBlocks = require './markdown-blocks'
|
||||
|
||||
module.exports = class TidyMarkdown extends Beautifier
|
||||
name: "Tidy Markdown"
|
||||
link: "https://github.com/slang800/tidy-markdown"
|
||||
options: {
|
||||
Markdown: false
|
||||
_: {
|
||||
beautifyCodeBlocks: true
|
||||
}
|
||||
Markdown: true
|
||||
}
|
||||
|
||||
beautify: (text, language, options) ->
|
||||
logger = @logger;
|
||||
return new @Promise((resolve, reject) ->
|
||||
tidyMarkdown = require 'tidy-markdown'
|
||||
cleanMarkdown = tidyMarkdown(text)
|
||||
resolve(cleanMarkdown)
|
||||
cleanMarkdown = tidyMarkdown(text, logger)
|
||||
if options.beautifyCodeBlocks
|
||||
beautifyCodeBlocks(cleanMarkdown)
|
||||
.then((t) => resolve(t))
|
||||
.catch((e) => reject(e))
|
||||
else
|
||||
resolve(cleanMarkdown)
|
||||
)
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
# global atom
|
||||
"use strict"
|
||||
pkg = require('../package')
|
||||
beautifier = require('./beautifier')
|
||||
|
||||
# Dependencies
|
||||
plugin = module.exports
|
||||
{CompositeDisposable} = require 'event-kit'
|
||||
_ = require("lodash")
|
||||
Beautifiers = require("./beautifiers")
|
||||
beautifier = new Beautifiers()
|
||||
defaultLanguageOptions = beautifier.options
|
||||
logger = require('./logger')(__filename)
|
||||
Promise = require('bluebird')
|
||||
|
|
|
@ -33,4 +33,8 @@ module.exports = {
|
|||
type: 'boolean'
|
||||
default: false
|
||||
description: 'Allows and disallows several constructs.'
|
||||
beautifyCodeBlocks:
|
||||
type: 'boolean'
|
||||
default: false
|
||||
description: 'Look for fenced code blocks (e.g. ```js) and beautify them using corresponding language rules'
|
||||
}
|
||||
|
|
|
@ -4686,6 +4686,21 @@
|
|||
"namespace": "markdown"
|
||||
}
|
||||
},
|
||||
"beautifyCodeBlocks": {
|
||||
"type": "boolean",
|
||||
"default": false,
|
||||
"description": "Look for fenced code blocks (e.g. ```js) and beautify them using corresponding language rules (Supported by Remark, Tidy Markdown)",
|
||||
"title": "Beautify Code Blocks",
|
||||
"beautifiers": [
|
||||
"Remark",
|
||||
"Tidy Markdown"
|
||||
],
|
||||
"key": "beautifyCodeBlocks",
|
||||
"language": {
|
||||
"name": "Markdown",
|
||||
"namespace": "markdown"
|
||||
}
|
||||
},
|
||||
"disabled": {
|
||||
"title": "Disable Beautifying Language",
|
||||
"order": -3,
|
||||
|
|
Loading…
Reference in New Issue