-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add attributes to fence blocks #2394
Changes from 6 commits
a591001
e295719
189b245
75a0f43
0a205f7
57fadac
7af7738
b03c2a1
297c764
a24f6e8
fbdc9c9
a816c5d
efd1b3c
a4ad389
c5bcfe6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
'use strict' | ||
|
||
module.exports = function (md, renderers, defaultRenderer) { | ||
function fence (state, startLine, endLine) { | ||
let pos = state.bMarks[startLine] + state.tShift[startLine] | ||
let max = state.eMarks[startLine] | ||
|
||
if (state.sCount[startLine] - state.blkIndent >= 4 || pos + 3 > max) { | ||
return false | ||
} | ||
|
||
const marker = state.src.charCodeAt(pos) | ||
if (!(marker === 96 || marker === 126)) { | ||
return false | ||
} | ||
|
||
let mem = pos | ||
pos = state.skipChars(pos, marker) | ||
|
||
let len = pos - mem | ||
if (len < 3) { | ||
return false | ||
} | ||
|
||
const markup = state.src.slice(mem, pos) | ||
const params = state.src.slice(pos, max) | ||
|
||
let nextLine = startLine | ||
let haveEndMarker = false | ||
|
||
while (true) { | ||
nextLine++ | ||
if (nextLine >= endLine) { | ||
break | ||
} | ||
|
||
pos = mem = state.bMarks[nextLine] + state.tShift[nextLine] | ||
max = state.eMarks[nextLine] | ||
|
||
if (pos < max && state.sCount[nextLine] < state.blkIndent) { | ||
break | ||
} | ||
if (state.src.charCodeAt(pos) !== marker || state.sCount[nextLine] - state.blkIndent >= 4) { | ||
continue | ||
} | ||
|
||
pos = state.skipChars(pos, marker) | ||
|
||
if (pos - mem < len) { | ||
continue | ||
} | ||
|
||
pos = state.skipSpaces(pos) | ||
|
||
if (pos >= max) { | ||
haveEndMarker = true | ||
break | ||
} | ||
} | ||
|
||
len = state.sCount[startLine] | ||
state.line = nextLine + (haveEndMarker ? 1 : 0) | ||
|
||
const parameters = {} | ||
let langType = '' | ||
let fileName = '' | ||
let firstLineNumber = 0 | ||
|
||
let match = /^(\w[-\w]*)?(?:\(((?:\s*\w[-\w]*(?:=(?:'(?:.*?[^\\])?'|"(?:.*?[^\\])?"|(?:[^'"][^\s]*)))?)*)\))?(?::([^:]*)(?::(\d+))?)?\s*$/.exec(params) | ||
if (match) { | ||
if (match[1]) { | ||
langType = match[1] | ||
} | ||
if (match[3]) { | ||
fileName = match[3] | ||
} | ||
if (match[4]) { | ||
firstLineNumber = parseInt(match[4], 10) | ||
} | ||
|
||
if (match[2]) { | ||
const params = match[2] | ||
const regex = /(\w[-\w]*)(?:=(?:'(.*?[^\\])?'|"(.*?[^\\])?"|([^'"][^\s]*)))?/g | ||
|
||
let name, value | ||
while ((match = regex.exec(params))) { | ||
name = match[1] | ||
value = match[2] || match[3] || match[4] || null | ||
|
||
const height = /^(\d+)h$/.exec(name) | ||
if (height && !value) { | ||
parameters.height = height[1] | ||
} else { | ||
parameters[name] = value | ||
} | ||
} | ||
} | ||
} | ||
|
||
let token | ||
if (renderers[langType]) { | ||
token = state.push(`${langType}_fence`, 'div', 0) | ||
} else { | ||
token = state.push('_fence', 'code', 0) | ||
} | ||
|
||
token.langType = langType | ||
token.fileName = fileName | ||
token.firstLineNumber = firstLineNumber | ||
token.parameters = parameters | ||
|
||
token.content = state.getLines(startLine + 1, nextLine, len, true) | ||
token.markup = markup | ||
token.map = [startLine, state.line] | ||
|
||
return true | ||
} | ||
|
||
md.block.ruler.before('fence', '_fence', fence, { | ||
alt: ['paragraph', 'reference', 'blockquote', 'list'] | ||
}) | ||
|
||
for (let name in renderers) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ZeroX-DG thanks! I did learn something today The for-of loop There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha, amazing right? I also discorvered the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did know the |
||
md.renderer.rules[`${name}_fence`] = (tokens, index) => renderers[name](tokens[index]) | ||
} | ||
|
||
if (defaultRenderer) { | ||
md.renderer.rules['_fence'] = (tokens, index) => defaultRenderer(tokens[index]) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggested change to fix a small display bug:
z-index: 10
Before:
After:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ZeroX-DG I removed the errors for every diagram (chart, flowchart, mermaid and sequence).
I've also fixed a bug with the line number...
But I can't replicated the visual bug you are getting...
Here what I get:
Here my preferences:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You preview font size is too small, you can set it to
24
and you will see the problemThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here my fix: