ソースを参照

Add optional compilers

simov 2 年 前
コミット
df7b2dd4d7

+ 1 - 0
.gitignore

@@ -7,3 +7,4 @@ package-lock.json
 /vendor/
 /*.zip
 /manifest.json
+/background/index-compilers.js

+ 30 - 0
background/compilers/commonmark.js

@@ -0,0 +1,30 @@
+
+md.compilers.commonmark = (() => {
+  var defaults = {
+    safe: false,
+    smart: false,
+  }
+
+  var description = {
+    safe: 'Raw HTML will not be rendered',
+    smart: [
+      'Straight quotes will be made curly',
+      '-- will be changed to an en dash',
+      '--- will be changed to an em dash',
+      'and ... will be changed to ellipses'
+    ].join('\n'),
+  }
+
+  var ctor = ({storage: {state}}) => ({
+    defaults,
+    description,
+    compile: (markdown) => ((
+      reader = new commonmark.Parser(),
+      writer = new commonmark.HtmlRenderer(state.commonmark)
+    ) =>
+      writer.render(reader.parse(markdown))
+    )()
+  })
+
+  return Object.assign(ctor, {defaults, description})
+})()

+ 30 - 0
background/compilers/markdown-it.js

@@ -0,0 +1,30 @@
+
+md.compilers['markdown-it'] = (() => {
+  var defaults = {
+    breaks: false,
+    html: true,
+    linkify: true,
+    typographer: false,
+    xhtmlOut: false,
+    langPrefix: 'language-',
+    quotes: '“”‘’'
+  }
+
+  var description = {
+    breaks: 'Convert \\n in paragraphs into <br>',
+    html: 'Enable HTML tags in source',
+    linkify: 'Autoconvert URL-like text to links',
+    typographer: 'Enable some language-neutral replacement + quotes beautification',
+    xhtmlOut: 'Use / to close single tags (<br />)'
+  }
+
+  var ctor = ({storage: {state}}) => ({
+    defaults,
+    description,
+    compile: (markdown) =>
+      markdownit(state['markdown-it'])
+        .render(markdown)
+  })
+
+  return Object.assign(ctor, {defaults, description})
+})()

+ 30 - 0
background/compilers/remarkable.js

@@ -0,0 +1,30 @@
+
+md.compilers.remarkable = (() => {
+  var defaults = {
+    breaks: false,
+    html: true,
+    linkify: true,
+    typographer: false,
+    xhtmlOut: false,
+    langPrefix: 'language-',
+    quotes: '“”‘’'
+  }
+
+  var description = {
+    breaks: 'Convert \\n in paragraphs into <br>',
+    html: 'Enable HTML tags in source',
+    linkify: 'Autoconvert URL-like text to links',
+    typographer: 'Enable some language-neutral replacement + quotes beautification',
+    xhtmlOut: 'Use / to close single tags (<br />)'
+  }
+
+  var ctor = ({storage: {state}}) => ({
+    defaults,
+    description,
+    compile: (markdown) =>
+      new Remarkable('full', state.remarkable)
+        .render(markdown)
+  })
+
+  return Object.assign(ctor, {defaults, description})
+})()

+ 58 - 0
background/compilers/showdown.js

@@ -0,0 +1,58 @@
+
+md.compilers.showdown = (() => {
+  var defaults = null // see below
+
+  var description = {
+    disableForced4SpacesIndentedSublists: 'Disables the requirement of indenting nested sublists by 4 spaces',
+    encodeEmails: 'Encode e-mail addresses through the use of Character Entities, transforming ASCII e-mail addresses into its equivalent decimal entities',
+    ghCodeBlocks: 'Turn on/off GFM fenced code blocks support',
+    ghCompatibleHeaderId: 'Generate header ids compatible with github style (spaces are replaced with dashes, a bunch of non alphanumeric chars are removed)',
+    ghMentions: 'Enables github @mentions',
+    literalMidWordUnderscores: 'Parse midword underscores as literal underscores',
+    noHeaderId: 'Turn on/off generated header id',
+    omitExtraWLInCodeBlocks: 'Omit the default extra whiteline added to code blocks',
+    parseImgDimensions: 'Turn on/off image dimension parsing',
+    prefixHeaderId: 'Specify a prefix to generated header ids',
+    requireSpaceBeforeHeadingText: 'Makes adding a space between `#` and the header text mandatory (GFM Style)',
+    simpleLineBreaks: 'Parses simple line breaks as <br> (GFM Style)',
+    simplifiedAutoLink: 'Turn on/off GFM autolink style',
+    smartIndentationFix: 'Tries to smartly fix indentation in es6 strings',
+    smoothLivePreview: 'Prevents weird effects in live previews due to incomplete input',
+    strikethrough: 'Turn on/off strikethrough support',
+    tables: 'Turn on/off tables support',
+    tablesHeaderId: 'Add an id to table headers',
+    tasklists: 'Turn on/off GFM tasklist support',
+    customizedHeaderId: 'Use text in curly braces as header id',
+    rawPrefixHeaderId: 'Prevent modifying the prefix',
+    rawHeaderId: 'Remove only spaces, \' and \' from generated header ids',
+    tablesHeaderId: 'Adds an id property to table headers tags',
+    openLinksInNewWindow: 'Open all links in new windows',
+    backslashEscapesHTMLTags: 'Support for HTML Tag escaping',
+    emoji: 'Enable emoji support',
+    ellipsis: 'Replaces three dots with the ellipsis unicode character',
+    metadata: 'Enable support for document metadata',
+    splitAdjacentBlockquotes: 'Split adjacent blockquote blocks',
+  }
+
+  var flavor = (name) => {
+    var options = showdown.getDefaultOptions()
+    var flavor = showdown.getFlavorOptions(name)
+    var result = {}
+    for (var key in options) {
+      result[key] = (flavor[key] !== undefined) ? flavor[key] : options[key]
+    }
+    return result
+  }
+
+  defaults = flavor('github')
+
+  var ctor = ({storage: {state}}) => ({
+    defaults,
+    description,
+    compile: (markdown) =>
+      new showdown.Converter(state.showdown)
+        .makeHtml(markdown)
+  })
+
+  return Object.assign(ctor, {defaults, description})
+})()

+ 0 - 1
background/index.js

@@ -1,7 +1,6 @@
 
 importScripts('/vendor/marked.min.js')
 importScripts('/vendor/remark.min.js')
-
 importScripts('/background/compilers/marked.js')
 importScripts('/background/compilers/remark.js')
 

+ 10 - 1
build/README.md

@@ -33,7 +33,7 @@ sh build/package.sh
 | github-markdown-css |  5.1.0
 | marked              |  4.1.1
 | mathjax             |  3.2.2
-| mermaid             |  9.1.6
+| mermaid             |  9.2.2
 | mithril             |  1.1.6
 | prismjs             | ^1.29.0
 | remark              | ^14.0.2
@@ -43,3 +43,12 @@ sh build/package.sh
 | remark-gfm          | ^3.0.1
 | remark-html         | ^15.0.1
 | remark-slug         | ^7.0.1
+
+## Additional Compilers
+
+| Module              | Version
+| :-                  | :-
+| commonmark          | 0.29.3
+| markdown-it         | 13.0.1
+| remarkable          | 1.7.4
+| showdown            | 2.1.0

+ 11 - 0
build/compilers/build.sh

@@ -0,0 +1,11 @@
+#!/bin/bash
+
+# set current working directory to directory of the shell script
+cd "$(dirname "$0")"
+
+curl https://cdnjs.cloudflare.com/ajax/libs/showdown/2.1.0/showdown.min.js --output ../../vendor/showdown.min.js
+curl https://cdnjs.cloudflare.com/ajax/libs/markdown-it/13.0.1/markdown-it.min.js --output ../../vendor/markdown-it.min.js
+curl https://cdnjs.cloudflare.com/ajax/libs/commonmark/0.29.3/commonmark.min.js --output ../../vendor/commonmark.min.js
+curl https://cdnjs.cloudflare.com/ajax/libs/remarkable/1.7.4/remarkable.min.js --output ../../vendor/remarkable.min.js
+
+node import.js

+ 37 - 0
build/compilers/import.js

@@ -0,0 +1,37 @@
+
+var fs = require('fs')
+var path = require('path')
+
+var fpath = {
+  background: path.resolve(__dirname, '../../background/index.js'),
+  compilers: path.resolve(__dirname, '../../background/index-compilers.js'),
+  manifest: path.resolve(__dirname, '../../manifest.json'),
+}
+
+var compilers = `
+importScripts('/vendor/showdown.min.js')
+importScripts('/vendor/markdown-it.min.js')
+importScripts('/vendor/remarkable.min.js')
+importScripts('/vendor/commonmark.min.js')
+importScripts('/background/compilers/showdown.js')
+importScripts('/background/compilers/markdown-it.js')
+importScripts('/background/compilers/remarkable.js')
+importScripts('/background/compilers/commonmark.js')
+`
+
+// background/index-compilers.js
+var source = fs.readFileSync(fpath.background, 'utf8')
+var lines = source.split('\n')
+fs.writeFileSync(
+  fpath.compilers,
+  lines.slice(0, 5).concat(compilers.split('\n')).concat(lines.slice(6)).join('\n'),
+  'utf8'
+)
+
+// manifest.json
+var source = fs.readFileSync(fpath.manifest, 'utf8')
+fs.writeFileSync(
+  fpath.manifest,
+  source.replace('/background/index.js', '/background/index-compilers.js'),
+  'utf8'
+)

+ 14 - 3
build/package.sh

@@ -4,6 +4,7 @@
 set -e
 
 browser=$1
+compilers=$2
 
 if [ -z "$browser" ]; then
   echo "Specify target browser"
@@ -14,6 +15,10 @@ fi
 # set current working directory to directory of the shell script
 cd "$(dirname "$0")"
 
+# cleanup
+rm -rf ../themes
+rm -rf ../vendor
+rm ../background/index-compilers.js
 mkdir -p ../themes
 mkdir -p ../vendor
 
@@ -27,7 +32,7 @@ sh prism/build.sh
 sh remark/build.sh
 sh themes/build.sh
 
-# archive
+# copy files
 mkdir -p tmp
 mkdir -p tmp/markdown-viewer
 cd ..
@@ -41,16 +46,22 @@ elif [ "$browser" = "firefox" ]; then
   cp manifest.firefox.json manifest.json
 fi
 
-# zip the markdown-viewer folder itself
+# archive the markdown-viewer folder itself
 if [ "$browser" = "chrome" ]; then
   cd build/tmp/
   zip -r ../../markdown-viewer.zip markdown-viewer
   cd ..
-# zip the contents of the markdown-viewer folder
+# archive the contents of the markdown-viewer folder
 elif [ "$browser" = "firefox" ]; then
   cd build/tmp/markdown-viewer/
   zip -r ../../../markdown-viewer.zip .
   cd ../../
 fi
 
+# cleanup
 rm -rf tmp/
+
+# compilers
+if [ "$compilers" = "compilers" ]; then
+  sh compilers/build.sh
+fi