Skip to content

Instantly share code, notes, and snippets.

@xream
Last active January 12, 2017 09:28
Show Gist options
  • Save xream/519c29eff6e0760a51e673312a0f4c06 to your computer and use it in GitHub Desktop.
Save xream/519c29eff6e0760a51e673312a0f4c06 to your computer and use it in GitHub Desktop.
"use strict"
Beautifier = require('./beautifier')
prettydiff = require("prettydiff")
_ = require('lodash')
module.exports = class VueBeautifier extends Beautifier
name: "Vue Beautifier"
options:
Vue: true
beautify: (text, language, options) ->
opt =
brace_style: "collapse-preserve-inline"
end_with_comma: true
# end_with_newline: true
indent_size: 2
max_preserve_newlines: 2
# preserve_newlines: false
# insert_final_newline: false
_.assign options, opt
return new @Promise((resolve, reject) ->
regexp = /(<(template|script|style)[^>]*>)((\s|\S)*?)<\/\2>/gi
resolve(text.replace(regexp, (match, begin, type, text) ->
lang = /lang\s*=\s*['"](\w+)["']/.exec(begin)?[1]
switch type
when "template"
return match
switch lang
when "pug", "jade"
match.replace(text, "\n" + require("pug-beautify")(text, options) + "\n")
when undefined
match.replace(text, "\n" + require("js-beautify").html(text, options) + "\n")
else
match
when "script"
match.replace(text, "\n" + require("js-beautify")(text, options) + "\n")
when "style"
return match
switch lang
when "sass", "scss"
options = _.merge options,
source: text
lang: "scss"
mode: "beautify"
match.replace(text, prettydiff.api(options)[0])
when "less"
options = _.merge options,
source: text
lang: "less"
mode: "beautify"
match.replace(text, prettydiff.api(options)[0])
when undefined
match.replace(text, "\n" + require("js-beautify").css(text, options) + "\n")
else
match
))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment