Commit bd2880bb authored by Douwe Maan's avatar Douwe Maan

Improve handling of code blocks containing triple backticks

parent d89f5616
...@@ -116,7 +116,19 @@ ...@@ -116,7 +116,19 @@
if (lang === 'plaintext') { if (lang === 'plaintext') {
lang = ''; lang = '';
} }
return `\`\`\`${lang}\n${text.trim()}\n\`\`\``; text = text.trim();
// Prefixes lines with 4 spaces if the code contains triple backticks
if (lang === '' && text.match(/^```/gm)) {
return text.split('\n').map((s) => {
s = s.trim();
if (s.length === 0) return '';
return ` ${s}`;
}).join('\n');
}
return `\`\`\`${lang}\n${text}\n\`\`\``;
}, },
'pre > code'(el, text) { 'pre > code'(el, text) {
// Don't wrap code blocks in `` // Don't wrap code blocks in ``
...@@ -207,8 +219,12 @@ ...@@ -207,8 +219,12 @@
return '-----'; return '-----';
}, },
'table'(el, text) { 'table'(el, text) {
const theadText = CopyAsGFM.nodeToGFM(el.querySelector('thead')); const theadEl = el.querySelector('thead');
const tbodyText = CopyAsGFM.nodeToGFM(el.querySelector('tbody')); const tbodyEl = el.querySelector('tbody');
if (!theadEl || !tbodyEl) return false;
const theadText = CopyAsGFM.nodeToGFM(theadEl);
const tbodyText = CopyAsGFM.nodeToGFM(tbodyEl);
return theadText + tbodyText; return theadText + tbodyText;
}, },
......
...@@ -270,13 +270,23 @@ describe 'Copy as GFM', feature: true, js: true do ...@@ -270,13 +270,23 @@ describe 'Copy as GFM', feature: true, js: true do
``` ```
GFM GFM
<<-GFM.strip_heredoc <<-GFM.strip_heredoc,
```ruby ```ruby
def foo def foo
bar bar
end end
``` ```
GFM GFM
<<-GFM.strip_heredoc
Foo
This is an example of GFM
```js
Code goes here
```
GFM
) )
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment