Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
288b2760
Commit
288b2760
authored
Dec 20, 2017
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Copy Mermaid graphs as GFM
parent
58a705aa
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
131 additions
and
14 deletions
+131
-14
app/assets/javascripts/behaviors/copy_as_gfm.js
app/assets/javascripts/behaviors/copy_as_gfm.js
+12
-0
app/assets/javascripts/render_mermaid.js
app/assets/javascripts/render_mermaid.js
+19
-1
lib/banzai/filter/mermaid_filter.rb
lib/banzai/filter/mermaid_filter.rb
+1
-10
spec/features/copy_as_gfm_spec.rb
spec/features/copy_as_gfm_spec.rb
+96
-0
spec/features/markdown_spec.rb
spec/features/markdown_spec.rb
+1
-1
spec/lib/banzai/filter/mermaid_filter_spec.rb
spec/lib/banzai/filter/mermaid_filter_spec.rb
+2
-2
No files found.
app/assets/javascripts/behaviors/copy_as_gfm.js
View file @
288b2760
...
@@ -74,6 +74,18 @@ const gfmRules = {
...
@@ -74,6 +74,18 @@ const gfmRules = {
return
`![
${
el
.
dataset
.
title
}
](
${
el
.
getAttribute
(
'
src
'
)}
)`
;
return
`![
${
el
.
dataset
.
title
}
](
${
el
.
getAttribute
(
'
src
'
)}
)`
;
},
},
},
},
MermaidFilter
:
{
'
svg.mermaid
'
(
el
,
text
)
{
const
sourceEl
=
el
.
querySelector
(
'
text.source
'
);
if
(
!
sourceEl
)
return
false
;
return
`\`\`\`mermaid\n
${
CopyAsGFM
.
nodeToGFM
(
sourceEl
)}
\n\`\`\``
;
},
'
svg.mermaid style, svg.mermaid g
'
(
el
,
text
)
{
// We don't want to include the content of these elements in the copied text.
return
''
;
},
},
MathFilter
:
{
MathFilter
:
{
'
pre.code.math[data-math-style=display]
'
(
el
,
text
)
{
'
pre.code.math[data-math-style=display]
'
(
el
,
text
)
{
return
`\`\`\`math\n
${
text
.
trim
()}
\n\`\`\``
;
return
`\`\`\`math\n
${
text
.
trim
()}
\n\`\`\``
;
...
...
app/assets/javascripts/render_mermaid.js
View file @
288b2760
...
@@ -24,7 +24,25 @@ export default function renderMermaid($els) {
...
@@ -24,7 +24,25 @@ export default function renderMermaid($els) {
});
});
$els
.
each
((
i
,
el
)
=>
{
$els
.
each
((
i
,
el
)
=>
{
mermaid
.
init
(
undefined
,
el
);
const
source
=
el
.
textContent
;
mermaid
.
init
(
undefined
,
el
,
(
id
)
=>
{
const
svg
=
document
.
getElementById
(
id
);
svg
.
classList
.
add
(
'
mermaid
'
);
// pre > code > svg
svg
.
closest
(
'
pre
'
).
replaceWith
(
svg
);
// We need to add the original source into the DOM to allow Copy-as-GFM
// to access it.
const
sourceEl
=
document
.
createElement
(
'
text
'
);
sourceEl
.
classList
.
add
(
'
source
'
);
sourceEl
.
setAttribute
(
'
display
'
,
'
none
'
);
sourceEl
.
textContent
=
source
;
svg
.
appendChild
(
sourceEl
);
});
});
});
}).
catch
((
err
)
=>
{
}).
catch
((
err
)
=>
{
Flash
(
`Can't load mermaid module:
${
err
}
`
);
Flash
(
`Can't load mermaid module:
${
err
}
`
);
...
...
lib/banzai/filter/mermaid_filter.rb
View file @
288b2760
...
@@ -2,16 +2,7 @@ module Banzai
...
@@ -2,16 +2,7 @@ module Banzai
module
Filter
module
Filter
class
MermaidFilter
<
HTML
::
Pipeline
::
Filter
class
MermaidFilter
<
HTML
::
Pipeline
::
Filter
def
call
def
call
doc
.
css
(
'pre[lang="mermaid"]'
).
add_class
(
'mermaid'
)
doc
.
css
(
'pre[lang="mermaid"] > code'
).
add_class
(
'js-render-mermaid'
)
doc
.
css
(
'pre[lang="mermaid"]'
).
add_class
(
'js-render-mermaid'
)
# The `<code></code>` blocks are added in the lib/banzai/filter/syntax_highlight_filter.rb
# We want to keep context and consistency, so we the blocks are added for all filters.
# Details: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/15107/diffs?diff_id=7962900#note_45495859
doc
.
css
(
'pre[lang="mermaid"]'
).
each
do
|
pre
|
document
=
pre
.
at
(
'code'
)
document
.
replace
(
document
.
content
)
end
doc
doc
end
end
...
...
spec/features/copy_as_gfm_spec.rb
View file @
288b2760
...
@@ -284,6 +284,102 @@ describe 'Copy as GFM', :js do
...
@@ -284,6 +284,102 @@ describe 'Copy as GFM', :js do
expect
(
output_gfm
.
strip
).
to
eq
(
gfm
.
strip
)
expect
(
output_gfm
.
strip
).
to
eq
(
gfm
.
strip
)
end
end
verify
(
'MermaidFilter: mermaid as converted from GFM to HTML'
,
<<-
GFM
.
strip_heredoc
```mermaid
graph TD;
A-->B;
```
GFM
)
aggregate_failures
(
'MermaidFilter: mermaid as transformed from HTML to SVG'
)
do
gfm
=
<<-
GFM
.
strip_heredoc
```mermaid
graph TD;
A-->B;
```
GFM
html
=
<<-
HTML
.
strip_heredoc
<svg id="mermaidChart1" xmlns="http://www.w3.org/2000/svg" height="100%" viewBox="0 0 87.234375 174" style="max-width:87.234375px;" class="mermaid">
<style>
.mermaid {
/* Flowchart variables */
/* Sequence Diagram variables */
/* Gantt chart variables */
/** Section styling */
/* Grid and axis */
/* Today line */
/* Task styling */
/* Default task */
/* Specific task settings for the sections*/
/* Active task */
/* Completed task */
/* Tasks on the critical line */
}
</style>
<g>
<g class="output">
<g class="clusters"></g>
<g class="edgePaths">
<g class="edgePath" style="opacity: 1;">
<path class="path" d="M33.6171875,52L33.6171875,77L33.6171875,102" marker-end="url(#arrowhead65)" style="fill:none"></path>
<defs>
<marker id="arrowhead65" viewBox="0 0 10 10" refX="9" refY="5" markerUnits="strokeWidth" markerWidth="8" markerHeight="6" orient="auto">
<path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath" style="stroke-width: 1; stroke-dasharray: 1, 0;"></path>
</marker>
</defs>
</g>
</g>
<g class="edgeLabels">
<g class="edgeLabel" style="opacity: 1;" transform="">
<g transform="translate(0,0)" class="label">
<foreignObject width="0" height="0">
<div xmlns="http://www.w3.org/1999/xhtml" style="display: inline-block; white-space: nowrap;">
<span class="edgeLabel"></span>
</div>
</foreignObject>
</g>
</g>
</g>
<g class="nodes">
<g class="node" id="A" transform="translate(33.6171875,36)" style="opacity: 1;">
<rect rx="0" ry="0" x="-13.6171875" y="-16" width="27.234375" height="32"></rect>
<g class="label" transform="translate(0,0)">
<g transform="translate(-3.6171875,-6)">
<foreignObject width="7.234375" height="12">
<div xmlns="http://www.w3.org/1999/xhtml" style="display: inline-block; white-space: nowrap;">A</div>
</foreignObject>
</g>
</g>
</g>
<g class="node" id="B" transform="translate(33.6171875,118)" style="opacity: 1;">
<rect rx="0" ry="0" x="-13.6171875" y="-16" width="27.234375" height="32">
</rect>
<g class="label" transform="translate(0,0)">
<g transform="translate(-3.6171875,-6)">
<foreignObject width="7.234375" height="12">
<div xmlns="http://www.w3.org/1999/xhtml" style="display: inline-block; white-space: nowrap;">B</div>
</foreignObject>
</g>
</g>
</g>
</g>
</g>
</g>
<text class="source" display="none">graph TD;
A-->B;
</text>
</svg>
HTML
output_gfm
=
html_to_gfm
(
html
)
expect
(
output_gfm
.
strip
).
to
eq
(
gfm
.
strip
)
end
verify
(
verify
(
'SanitizationFilter'
,
'SanitizationFilter'
,
...
...
spec/features/markdown_spec.rb
View file @
288b2760
...
@@ -71,7 +71,7 @@ describe 'GitLab Markdown' do
...
@@ -71,7 +71,7 @@ describe 'GitLab Markdown' do
it
'parses mermaid code block'
do
it
'parses mermaid code block'
do
aggregate_failures
do
aggregate_failures
do
expect
(
doc
).
to
have_selector
(
'pre
.
code.js-render-mermaid'
)
expect
(
doc
).
to
have_selector
(
'pre
[lang=mermaid] >
code.js-render-mermaid'
)
end
end
end
end
...
...
spec/lib/banzai/filter/mermaid_filter_spec.rb
View file @
288b2760
...
@@ -3,9 +3,9 @@ require 'spec_helper'
...
@@ -3,9 +3,9 @@ require 'spec_helper'
describe
Banzai
::
Filter
::
MermaidFilter
do
describe
Banzai
::
Filter
::
MermaidFilter
do
include
FilterSpecHelper
include
FilterSpecHelper
it
'adds `js-render-mermaid` class to the `
pr
e` tag'
do
it
'adds `js-render-mermaid` class to the `
cod
e` tag'
do
doc
=
filter
(
"<pre class='code highlight js-syntax-highlight mermaid' lang='mermaid' v-pre='true'><code>graph TD;
\n
A-->B;
\n
</code></pre>"
)
doc
=
filter
(
"<pre class='code highlight js-syntax-highlight mermaid' lang='mermaid' v-pre='true'><code>graph TD;
\n
A-->B;
\n
</code></pre>"
)
result
=
doc
.
xpath
(
'descendant-or-self::pr
e'
).
first
result
=
doc
.
css
(
'cod
e'
).
first
expect
(
result
[
:class
]).
to
include
(
'js-render-mermaid'
)
expect
(
result
[
:class
]).
to
include
(
'js-render-mermaid'
)
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment