optimize extension, remove markdown-it plugin

This commit is contained in:
Felix Wu 2018-08-21 11:05:51 +02:00
parent beb695e53c
commit d443be2a47
7 changed files with 30 additions and 158 deletions

12
package-lock.json generated
View File

@ -739,11 +739,14 @@
"integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q=="
},
"abcjs": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/abcjs/-/abcjs-5.2.0.tgz",
"integrity": "sha512-VBTgtp2esnv6RR2N6Nsa+V7VE07H/TLtUju+Orh6anud5KDfUE6eEM8OWt2oKMwKR2zsrY8Y5KyH541uyV2DEw==",
"version": "git+https://git@github.com/flxwu/abcjs.git#5842d8efd7237c7e5c82077773de112695cf973c",
"requires": {
"midi": "git+https://github.com/paulrosen/MIDI.js.git#e593ffef81a0350f99448e3ab8111957145ff6b2"
},
"dependencies": {
"midi": {
"version": "git+https://github.com/paulrosen/MIDI.js.git#e593ffef81a0350f99448e3ab8111957145ff6b2"
}
}
},
"accepts": {
@ -11559,9 +11562,6 @@
}
}
},
"midi": {
"version": "git+https://github.com/paulrosen/MIDI.js.git#e593ffef81a0350f99448e3ab8111957145ff6b2"
},
"miller-rabin": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/miller-rabin/-/miller-rabin-4.0.1.tgz",

View File

@ -25,7 +25,7 @@
},
"dependencies": {
"@vue/test-utils": "^1.0.0-beta.16",
"abcjs": "^5.2.0",
"abcjs": "git+https://git@github.com/flxwu/abcjs.git",
"aws-sdk": "^2.133.0",
"babel-runtime": "^6.26.0",
"bezier-easing": "^1.1.0",

View File

@ -1,7 +1,7 @@
const zero = {
// Markdown extensions
markdown: {
abbr: false,
abbr: true,
breaks: false,
deflist: false,
del: false,
@ -28,7 +28,7 @@ const zero = {
See https://abcjs.net/
*/
abc: {
enabled: false,
enabled: true,
},
/*
Katex extension
@ -98,7 +98,7 @@ export default {
enabled: true,
},
abc: {
enabled: false,
enabled: true,
},
}],
};

View File

@ -0,0 +1,19 @@
/* eslint-disable */
import abcjs from 'abcjs';
import markdownItNotesSheet from './libs/markdownItNotesSheet';
import extensionSvc from '../services/extensionSvc';
const render = (elt) => {
const abcContent = elt.textContent;
elt.parentNode.parentNode.id = 'abcSheetPaper';
abcjs.renderAbc('abcSheetPaper', abcContent, {});
}
extensionSvc.onGetOptions((options, properties) => {
options.abc = properties.extensions.abc.enabled;
});
extensionSvc.onSectionPreview((elt) => {
elt.querySelectorAll('.prism.language-abc')
.cl_each(notationElt => render(notationElt));
});

View File

@ -1,23 +0,0 @@
import abcjs from 'abcjs';
import markdownItNotesSheet from './libs/markdownItNotesSheet';
import extensionSvc from '../services/extensionSvc';
let abc;
extensionSvc.onGetOptions((options, properties) => {
options.abc = properties.extensions.abc.enabled;
});
extensionSvc.onInitConverter(2, (markdown, options) => {
if (options.abc) {
markdown.use(markdownItNotesSheet, (val) => {
abc = val;
});
}
});
extensionSvc.onSectionPreview(() => {
if (document.querySelector('#abcSheetPaper') != null && abc != null) {
abcjs.renderAbc('abcSheetPaper', abc, {});
}
});

View File

@ -1,5 +1,5 @@
import './emojiExtension';
import './abcNotationExtension';
import './abcExtension';
import './katexExtension';
import './markdownExtension';
import './mermaidExtension';

View File

@ -1,124 +0,0 @@
/* eslint-disable no-continue */
// a constant to hold the abc string
const abc = [];
// the callBack given from abcNotationExtension.js
// to pass the abc string up to a point
// where the DOM is already rendered
let callbackFunc;
function abcNotation(state, startLine, endLine, silent) {
const validateParams = params => params.trim().match(/^abc$/);
let nextLine;
let autoClosed = false;
let start = state.bMarks[startLine] + state.tShift[startLine];
let max = state.eMarks[startLine];
// Check out the first character quickly,
// this filters out all non-codeblocks
const marker = state.src.charCodeAt(start);
if (marker !== 0x7e /* ~ */ && marker !== 0x60 /* ` */) {
return false;
}
// Check out the rest of the marker string
const markerStart = start;
start = state.skipChars(start, marker);
const markerLength = start - markerStart;
if (markerLength < 3) {
return false;
}
const markup = state.src.slice(markerLength, start);
const params = state.src.slice(start, max);
if (!validateParams(params)) {
return false;
}
// Since start is found, we can report success here in validation mode
if (silent) {
return true;
}
// Search for the end of the block
nextLine = startLine;
for (;;) {
nextLine += 1;
if (nextLine >= endLine) {
// unclosed block should be autoclosed by end of document.
// also block seems to be autoclosed by end of parent
break;
}
start = state.bMarks[nextLine] + state.tShift[nextLine];
max = state.eMarks[nextLine];
if (start < max && state.sCount[nextLine] < state.blkIndent) {
// non-empty line with negative indent should stop the list:
// - ```
// test
break;
}
if (marker !== state.src.charCodeAt(start)) {
continue;
}
if (state.sCount[nextLine] - state.blkIndent >= 4) {
// closing fence should be indented less than 4 spaces
continue;
}
start = state.skipChars(start, marker);
// closing fence must be at least as long as the opening one
if (start - markerStart < markerLength) {
continue;
}
// make sure tail has spaces only
start = state.skipSpaces(start);
if (start < max) {
continue;
}
autoClosed = true;
// found!
break;
}
const oldParent = state.parentType;
const oldLineMax = state.lineMax;
state.parentType = 'container';
// If a fence has heading spaces, they should be removed from its inner block
// markerLength = state.sCount[startLine];
// this will prevent lazy continuations from ever going past our end marker
state.lineMax = nextLine;
const token = state.push('fence_abc_open', 'div', 1);
token.info = params;
token.markup = markup;
token.block = true;
token.map = [startLine, nextLine];
abc.push(state.getLines(startLine + 1, nextLine, markerLength, true));
state.parentType = oldParent;
state.lineMax = oldLineMax;
state.line = nextLine + (autoClosed ? 1 : 0);
callbackFunc(abc[0]);
return true;
}
export default (md, callback) => {
callbackFunc = callback;
md.block.ruler.before('fence', 'fence_abc', abcNotation);
md.renderer.rules.fence_abc_open = (tokens, idx, _options, env, self) => {
tokens[idx].attrPush(['id', 'abcSheetPaper']);
return self.renderToken(tokens, idx, _options, env, self);
};
};