Skip to content

Commit

Permalink
Add GitLab math block support
Browse files Browse the repository at this point in the history
  • Loading branch information
fxha committed May 17, 2020
1 parent 6892a14 commit f324d76
Show file tree
Hide file tree
Showing 21 changed files with 1,788 additions and 1,367 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ module.exports = {
'no-new': "off",
'no-mixed-operators': "off",
'no-prototype-builtins': "off",
"no-return-await": "off"
'no-return-await': "off",
'accessor-pairs': "off"
},
settings: {
'import/resolver': {
Expand Down
108 changes: 54 additions & 54 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,135 +39,135 @@
"arg": "^4.1.3",
"axios": "^0.19.2",
"ced": "^1.0.0",
"chokidar": "^3.3.1",
"codemirror": "^5.52.0",
"command-exists": "^1.2.8",
"dayjs": "^1.8.23",
"chokidar": "^3.4.0",
"codemirror": "^5.53.2",
"command-exists": "^1.2.9",
"dayjs": "^1.8.27",
"dom-autoscroller": "^2.3.4",
"dompurify": "^2.0.8",
"dompurify": "^2.0.11",
"dragula": "^3.7.2",
"electron-is-accelerator": "^0.1.2",
"electron-log": "^4.1.0",
"electron-is-accelerator": "^0.2.0",
"electron-log": "^4.1.2",
"electron-store": "^5.1.1",
"electron-window-state": "^5.0.3",
"element-resize-detector": "^1.2.1",
"element-ui": "^2.13.0",
"element-ui": "^2.13.1",
"execall": "^2.0.0",
"file-icons-js": "^1.0.3",
"flowchart.js": "^1.13.0",
"fontmanager-redux": "^0.4.0",
"fs-extra": "^8.1.0",
"fs-extra": "^9.0.0",
"fuzzaldrin": "^2.1.0",
"github-markdown-css": "^3.0.1",
"html-tags": "^3.1.0",
"iconv-lite": "^0.5.1",
"iso-639-1": "^2.1.1",
"iso-639-1": "^2.1.3",
"joplin-turndown-plugin-gfm": "^1.0.12",
"katex": "^0.11.1",
"keyboard-layout": "^2.0.16",
"keytar": "5.4.0",
"mermaid": "^8.4.8",
"keytar": "5.6.0",
"mermaid": "^8.5.0",
"plist": "^3.0.1",
"popper.js": "^1.16.1",
"prismjs": "^1.19.0",
"prismjs": "^1.20.0",
"snabbdom": "^0.7.4",
"snabbdom-to-html": "^5.1.1",
"source-map-support": "^0.5.16",
"snabbdom-to-html": "^6.0.0",
"source-map-support": "^0.5.19",
"turndown": "^6.0.0",
"underscore": "^1.9.2",
"underscore": "^1.10.2",
"unsplash-js": "^6.0.0",
"vega": "^5.10.0",
"vega-embed": "^6.4.0",
"vega-lite": "^4.7.0",
"vega": "^5.11.1",
"vega-embed": "^6.7.1",
"vega-lite": "^4.12.0",
"view-image": "^0.0.1",
"vscode-ripgrep": "^1.5.8",
"vue": "^2.6.11",
"vue-electron": "^1.0.6",
"vue-router": "^3.1.6",
"vuex": "^3.1.3",
"vuex": "^3.4.0",
"webfontloader": "^1.6.28"
},
"devDependencies": {
"@babel/core": "^7.8.7",
"@babel/core": "^7.9.6",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-export-default-from": "^7.8.3",
"@babel/plugin-proposal-function-bind": "^7.8.3",
"@babel/plugin-syntax-class-properties": "^7.8.3",
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/plugin-transform-runtime": "^7.8.3",
"@babel/preset-env": "^7.8.7",
"@babel/register": "^7.8.6",
"@babel/runtime": "^7.8.7",
"@markedjs/html-differ": "^3.0.0",
"@babel/plugin-transform-runtime": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@babel/register": "^7.9.0",
"@babel/runtime": "^7.9.6",
"@markedjs/html-differ": "^3.0.2",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.0.6",
"babel-loader": "^8.1.0",
"babel-plugin-component": "^1.1.1",
"babel-plugin-istanbul": "^6.0.0",
"cfonts": "^2.8.1",
"cfonts": "^2.8.2",
"chai": "^4.2.0",
"chalk": "^3.0.0",
"chalk": "^4.0.0",
"cheerio": "^1.0.0-rc.3",
"copy-webpack-plugin": "^5.1.1",
"cross-env": "^7.0.2",
"css-loader": "^3.4.2",
"css-loader": "^3.5.3",
"del": "^5.1.0",
"devtron": "^1.4.0",
"dotenv": "^8.2.0",
"electron": "^8.1.1",
"electron-builder": "^22.4.1",
"electron-devtools-installer": "^2.2.4",
"electron-rebuild": "^1.10.1",
"electron-updater": "^4.2.5",
"eslint": "^6.8.0",
"eslint-config-standard": "^14.1.0",
"electron": "^8.3.0",
"electron-builder": "^22.6.0",
"electron-devtools-installer": "^3.0.0",
"electron-rebuild": "^1.11.0",
"electron-updater": "^4.3.1",
"eslint": "^7.0.0",
"eslint-config-standard": "^14.1.1",
"eslint-friendly-formatter": "^4.0.1",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-loader": "^3.0.3",
"eslint-plugin-html": "^6.0.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-node": "^11.0.0",
"eslint-loader": "^4.0.2",
"eslint-plugin-html": "^6.0.2",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"eslint-plugin-vue": "^6.2.2",
"esm": "^3.2.25",
"file-loader": "^6.0.0",
"git-revision-webpack-plugin": "^3.0.4",
"git-revision-webpack-plugin": "^3.0.6",
"html-webpack-plugin": "^3.2.0",
"imports-loader": "^0.8.0",
"karma": "^4.4.1",
"karma": "^5.0.7",
"karma-chai": "^0.1.0",
"karma-coverage": "^2.0.1",
"karma-coverage": "^2.0.2",
"karma-electron": "6.3.0",
"karma-mocha": "^1.3.0",
"karma-mocha": "^2.0.1",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "0.0.32",
"karma-webpack": "^4.0.2",
"license-checker": "^25.0.1",
"marked": "^0.8.0",
"mini-css-extract-plugin": "^0.9.0",
"mocha": "^7.1.0",
"mocha": "^7.1.2",
"multispinner": "^0.2.1",
"node-fetch": "^2.6.0",
"node-loader": "^0.6.0",
"postcss-loader": "^3.0.0",
"postcss-preset-env": "^6.7.0",
"raw-loader": "^4.0.0",
"raw-loader": "^4.0.1",
"require-dir": "^1.2.0",
"spectron": "^10.0.1",
"style-loader": "^1.1.3",
"svg-sprite-loader": "^4.2.1",
"style-loader": "^1.2.1",
"svg-sprite-loader": "^4.3.0",
"svgo": "^1.3.2",
"svgo-loader": "^2.2.1",
"to-string-loader": "^1.1.6",
"url-loader": "^4.0.0",
"url-loader": "^4.1.0",
"vue-html-loader": "^1.2.4",
"vue-loader": "^15.9.0",
"vue-loader": "^15.9.2",
"vue-style-loader": "^4.1.2",
"vue-template-compiler": "^2.6.11",
"webpack": "^4.42.0",
"webpack-bundle-analyzer": "^3.6.1",
"webpack": "^4.43.0",
"webpack-bundle-analyzer": "^3.7.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3",
"webpack-dev-server": "^3.11.0",
"webpack-hot-middleware": "^2.25.0",
"webpack-merge": "^4.2.2"
},
Expand Down
5 changes: 5 additions & 0 deletions src/main/preferences/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@
"type": "boolean",
"default": false
},
"isGitlabCompatibilityEnabled": {
"description": "Markdown-Enable GitLab compatibility mode.",
"type": "boolean",
"default": false
},
"sequenceTheme": {
"description": "Markdown--Sequence diagram theme",
"enum": [
Expand Down
3 changes: 2 additions & 1 deletion src/muya/lib/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ export const MUYA_DEFAULT_OPTION = {

// Markdown extensions
superSubScript: false,
footnote: false
footnote: false,
isGitlabCompatibilityEnabled: false
}

// export const DIAGRAM_TEMPLATE = {
Expand Down
3 changes: 3 additions & 0 deletions src/muya/lib/contentState/codeBlockCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const codeBlockCtrl = ContentState => {

ContentState.prototype.selectLanguage = function (paragraph, lang) {
const block = this.getBlock(paragraph.id)
if (lang === 'math' && this.isGitlabCompatibilityEnabled && this.updateMathBlock(block)) {
return
}
this.updateCodeLanguage(block, lang)
}

Expand Down
49 changes: 44 additions & 5 deletions src/muya/lib/contentState/containerCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ const FUNCTION_TYPE_LANG = {
}

const containerCtrl = ContentState => {
ContentState.prototype.createContainerBlock = function (functionType, value = '') {
ContentState.prototype.createContainerBlock = function (functionType, value = '', style = undefined) {
const figureBlock = this.createBlock('figure', {
functionType
})

if (functionType === 'multiplemath') {
if (style === undefined) {
figureBlock.mathStyle = this.isGitlabCompatibilityEnabled ? 'gitlab' : ''
}
figureBlock.mathStyle = style
}

const { preBlock, preview } = this.createPreAndPreview(functionType, value)
this.appendChild(figureBlock, preBlock)
this.appendChild(figureBlock, preview)
Expand Down Expand Up @@ -56,11 +63,18 @@ const containerCtrl = ContentState => {
return { preBlock, preview }
}

ContentState.prototype.initContainerBlock = function (functionType, block) { // p block
ContentState.prototype.initContainerBlock = function (functionType, block, style = undefined) { // p block
block.type = 'figure'
block.functionType = functionType
block.children = []

if (functionType === 'multiplemath') {
if (style === undefined) {
block.mathStyle = this.isGitlabCompatibilityEnabled ? 'gitlab' : ''
}
block.mathStyle = style
}

const { preBlock, preview } = this.createPreAndPreview(functionType)

this.appendChild(block, preBlock)
Expand All @@ -84,11 +98,36 @@ const containerCtrl = ContentState => {
}

ContentState.prototype.updateMathBlock = function (block) {
const functionType = 'multiplemath'
const { type } = block
if (type !== 'p') return false

// TODO(GitLab): Allow "functionType" 'languageInput' to convert an existing
// code block into math block.
if (type === 'span' && block.functionType === 'paragraphContent') {
const isMathBlock = !!block.text.match(/^`{3,}math\s*/)
if (isMathBlock) {
const result = this.initContainerBlock(functionType, block, 'gitlab')
if (result) {
// Set cursor at the first line
const { key } = result
const offset = 0
this.cursor = {
start: { key, offset },
end: { key, offset }
}

// Force render
this.partialRender()
return result
}
}
return false
} else if (type !== 'p') {
return false
}

const { text } = block.children[0]
const functionType = 'multiplemath'
return text.trim() === '$$' ? this.initContainerBlock(functionType, block) : false
return text.trim() === '$$' ? this.initContainerBlock(functionType, block, '') : false
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/muya/lib/contentState/copyCutCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ const copyCutCtrl = ContentState => {

const table = this.createTableInFigure({ rows: row, columns: column }, tableContents)
this.appendChild(figureBlock, table)
const listIndentation = this.listIndentation
const markdown = new ExportMarkdown([figureBlock], listIndentation).generate()
const { isGitlabCompatibilityEnabled, listIndentation } = this
const markdown = new ExportMarkdown([figureBlock], listIndentation, isGitlabCompatibilityEnabled).generate()

event.clipboardData.setData('text/html', '')
event.clipboardData.setData('text/plain', markdown)
Expand Down Expand Up @@ -281,7 +281,9 @@ const copyCutCtrl = ContentState => {
case 'copyAsHtml': {
event.clipboardData.setData('text/html', '')
event.clipboardData.setData('text/plain', getSanitizeHtml(text, {
superSubScript: this.muya.options.superSubScript
superSubScript: this.muya.options.superSubScript,
footnote: this.muya.options.footnote,
isGitlabCompatibilityEnabled: this.muya.options.isGitlabCompatibilityEnabled
}))
break
}
Expand All @@ -290,8 +292,8 @@ const copyCutCtrl = ContentState => {
const block = typeof copyInfo === 'string' ? this.getBlock(copyInfo) : copyInfo
if (!block) return
const anchor = this.getAnchor(block)
const listIndentation = this.listIndentation
const markdown = new ExportMarkdown([anchor], listIndentation).generate()
const { isGitlabCompatibilityEnabled, listIndentation } = this
const markdown = new ExportMarkdown([anchor], listIndentation, isGitlabCompatibilityEnabled).generate()
event.clipboardData.setData('text/html', '')
event.clipboardData.setData('text/plain', markdown)
break
Expand Down
8 changes: 6 additions & 2 deletions src/muya/lib/contentState/paragraphCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,12 @@ const paragraphCtrl = ContentState => {
lang
})

const listIndentation = this.listIndentation
const markdown = new ExportMarkdown(children.slice(startIndex, endIndex + 1), listIndentation).generate()
const { isGitlabCompatibilityEnabled, listIndentation } = this
const markdown = new ExportMarkdown(
children.slice(startIndex, endIndex + 1),
listIndentation,
isGitlabCompatibilityEnabled
).generate()
const codeContent = this.createBlock('span', {
text: markdown,
lang,
Expand Down
4 changes: 2 additions & 2 deletions src/muya/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ class Muya {

getMarkdown () {
const blocks = this.contentState.getBlocks()
const listIndentation = this.contentState.listIndentation
return new ExportMarkdown(blocks, listIndentation).generate()
const { isGitlabCompatibilityEnabled, listIndentation } = this.contentState
return new ExportMarkdown(blocks, listIndentation, isGitlabCompatibilityEnabled).generate()
}

getHistory () {
Expand Down
1 change: 1 addition & 0 deletions src/muya/lib/parser/marked/blockRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const block = {
// extra
frontmatter: /^(?:(?:---\n([\s\S]+?)---)|(?:\+\+\+\n([\s\S]+?)\+\+\+)|(?:;;;\n([\s\S]+?);;;)|(?:\{\n([\s\S]+?)\}))(?:\n{2,}|\n{1,2}$)/,
multiplemath: /^\$\$\n([\s\S]+?)\n\$\$(?:\n+|$)/,
multiplemathGitlab: /^ {0,3}(`{3,})math\n(?:(|[\s\S]*?)\n)(?: {0,3}\1`* *(?:\n+|$)|$)/, // Math inside a code block (GitLab display math)
footnote: /^\[\^([^\^\[\]\s]+?)\]:[\s\S]+?(?=\n *\n {0,3}[^ ]+|$)/
}

Expand Down

0 comments on commit f324d76

Please sign in to comment.