浏览代码

Move compilers config into the respective compiler modules

simov 8 年之前
父节点
当前提交
58301c253e
共有 4 个文件被更改,包括 54 次插入61 次删除
  1. 21 26
      background/background.js
  2. 13 4
      background/marked.js
  3. 13 4
      background/remark.js
  4. 7 27
      content/popup.js

+ 21 - 26
background/background.js

@@ -2,25 +2,26 @@
 // chrome.storage.sync.clear()
 // chrome.permissions.getAll((p) => chrome.permissions.remove({origins: p.origins}))
 
-chrome.storage.sync.get((res) => {
-  var match = '\\.(?:markdown|mdown|mkdn|md|mkd|mdwn|mdtxt|mdtext|text)(?:#.*)?$'
-
-  var defaults = {
-    theme: 'github',
-    compiler: 'marked',
-    marked: md.marked.defaults,
-    remark: md.remark.defaults,
-    content: {
-      toc: false,
-      scroll: true
-    },
-    raw: false,
-    match,
-    origins: {
-      'file://': match
-    }
+var match = '\\.(?:markdown|mdown|mkdn|md|mkd|mdwn|mdtxt|mdtext|text)(?:#.*)?$'
+
+var defaults = {
+  theme: 'github',
+  compiler: 'marked',
+  content: {
+    toc: false,
+    scroll: true
+  },
+  raw: false,
+  match,
+  origins: {
+    'file://': match
   }
+}
+Object.keys(md).forEach((compiler) => {
+  defaults[compiler] = md[compiler].defaults
+})
 
+chrome.storage.sync.get((res) => {
   var options = !Object.keys(res).length ? defaults : res
 
   // v2.2 -> v2.3
@@ -160,7 +161,8 @@ chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
     chrome.storage.sync.get((res) => {
       sendResponse({
         compiler: res.compiler, options: res[res.compiler],
-        content: res.content, theme: res.theme, raw: res.raw
+        content: res.content, theme: res.theme, raw: res.raw,
+        compilers: md
       })
     })
   }
@@ -177,14 +179,7 @@ chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
     sendMessage({message: 'reload'})
   }
   else if (req.message === 'defaults') {
-    chrome.storage.sync.set({
-      theme: 'github',
-      compiler: 'marked',
-      marked: md.marked.defaults,
-      remark: md.remark.defaults,
-      content: {toc: false, scroll: true},
-      raw: false
-    }, sendResponse)
+    chrome.storage.sync.set(defaults, sendResponse)
     sendMessage({message: 'reload'})
   }
   else if (req.message === 'theme') {

+ 13 - 4
background/marked.js

@@ -3,14 +3,23 @@ var md = {}
 
 md.marked = {
   defaults: {
-    gfm: true,
-    tables: true,
     breaks: false,
+    gfm: true,
     pedantic: false,
     sanitize: false,
     smartLists: false,
-    langPrefix: 'language-', // prism
-    smartypants: false
+    smartypants: false,
+    tables: true,
+    langPrefix: 'language-' // prism
+  },
+  description: {
+    breaks: 'Enable GFM line breaks\n(requires the gfm option to be true)',
+    gfm: 'Enable GFM\n(GitHub Flavored Markdown)',
+    pedantic: 'Don\'t fix any of the original markdown\nbugs or poor behavior',
+    sanitize: 'Ignore any HTML\nthat has been input',
+    smartLists: 'Use smarter list behavior\nthan the original markdown',
+    smartypants: 'Use "smart" typographic punctuation\nfor things like quotes and dashes',
+    tables: 'Enable GFM tables\n(requires the gfm option to be true)'
   },
   compile: (markdown, sendResponse) => {
     chrome.storage.sync.get('marked', (res) => {

+ 13 - 4
background/remark.js

@@ -1,15 +1,24 @@
 
 md.remark = {
   defaults: {
-    gfm: true,
-    yaml: true,
+    breaks: false,
     commonmark: false,
     footnotes: false,
+    gfm: true,
     pedantic: false,
-    breaks: false,
-    sanitize: false
+    sanitize: false,
+    yaml: true
     // blocks (Array.<string>, default: list of block HTML elements)
   },
+  description: {
+    breaks: 'Exposes newline characters inside paragraphs as breaks',
+    commonmark: 'Toggle CommonMark mode',
+    footnotes: 'Toggle reference footnotes and inline footnotes',
+    gfm: 'Toggle GFM (GitHub Flavored Markdown)',
+    pedantic: 'Don\'t fix any of the original markdown\nbugs or poor behavior',
+    sanitize: 'Toggle HTML tag rendering',
+    yaml: 'Enables raw YAML front matter to be detected at the top'
+  },
   compile: (markdown, sendResponse) => {
     chrome.storage.sync.get('remark', (res) => {
       var html = remark.unified()

+ 7 - 27
content/popup.js

@@ -6,7 +6,8 @@ var state = {
   theme: '',
   themes: [],
   raw: false,
-  tab: ''
+  tab: '',
+  compilers: {}
 }
 
 var events = {
@@ -17,7 +18,7 @@ var events = {
 
   compiler: {
     name: (e) => {
-      state.compiler = ui.compilers[e.target.selectedIndex]
+      state.compiler = Object.keys(state.compilers)[e.target.selectedIndex]
       chrome.runtime.sendMessage({
         message: 'compiler.name',
         compiler: state.compiler
@@ -77,30 +78,7 @@ var ui = {
   tabs: [
     'theme', 'compiler', 'content'
   ],
-  compilers: [
-    'marked', 'remark'
-  ],
   description: {
-    compiler: {
-      marked: {
-        breaks: 'Enable GFM line breaks\n(requires the gfm option to be true)',
-        gfm: 'Enable GFM\n(GitHub Flavored Markdown)',
-        pedantic: 'Don\'t fix any of the original markdown\nbugs or poor behavior',
-        sanitize: 'Ignore any HTML\nthat has been input',
-        smartLists: 'Use smarter list behavior\nthan the original markdown',
-        smartypants: 'Use "smart" typographic punctuation\nfor things like quotes and dashes',
-        tables: 'Enable GFM tables\n(requires the gfm option to be true)'
-      },
-      remark: {
-        breaks: 'Exposes newline characters inside paragraphs as breaks',
-        commonmark: 'Toggle CommonMark mode',
-        footnotes: 'Toggle reference footnotes and inline footnotes',
-        gfm: 'Toggle GFM (GitHub Flavored Markdown)',
-        pedantic: 'Don\'t fix any of the original markdown\nbugs or poor behavior',
-        sanitize: 'Toggle HTML tag rendering',
-        yaml: 'Enables raw YAML front matter to be detected at the top'
-      }
-    },
     content: {
       scroll: 'Remember scroll position',
       toc: 'Generate Table of Contents'
@@ -120,6 +98,8 @@ function init (res) {
 
   state.raw = res.raw
   state.tab = localStorage.getItem('tab') || 'theme'
+  state.compilers = res.compilers
+
   m.redraw()
 }
 
@@ -167,7 +147,7 @@ m.mount(document.querySelector('body'), {
 
         // compiler
         m('.mdl-tabs__panel #tab-compiler', {class: state.tab === 'compiler' ? 'is-active' : undefined},
-          m('select.mdl-shadow--2dp', {onchange: events.compiler.name}, ui.compilers.map((name) =>
+          m('select.mdl-shadow--2dp', {onchange: events.compiler.name}, Object.keys(state.compilers).map((name) =>
             m('option', {selected: state.compiler === name}, name)
           )),
           m('.scroll', {class: state.compiler},
@@ -177,7 +157,7 @@ m.mount(document.querySelector('body'), {
               m('.mdl-cell',
                 m('label.mdl-switch mdl-js-switch mdl-js-ripple-effect',
                   {oncreate, onupdate: onupdate('compiler', key),
-                  title: ui.description.compiler[state.compiler][key]},
+                  title: state.compilers[state.compiler].description[key]},
                   m('input[type="checkbox"].mdl-switch__input', {
                     name: key,
                     checked: state.options[key],