Commit bd2880bb authored by Douwe Maan's avatar Douwe Maan

Improve handling of code blocks containing triple backticks

parent d89f5616
......@@ -116,7 +116,19 @@
if (lang === 'plaintext') {
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) {
// Don't wrap code blocks in ``
......@@ -207,8 +219,12 @@
return '-----';
},
'table'(el, text) {
const theadText = CopyAsGFM.nodeToGFM(el.querySelector('thead'));
const tbodyText = CopyAsGFM.nodeToGFM(el.querySelector('tbody'));
const theadEl = el.querySelector('thead');
const tbodyEl = el.querySelector('tbody');
if (!theadEl || !tbodyEl) return false;
const theadText = CopyAsGFM.nodeToGFM(theadEl);
const tbodyText = CopyAsGFM.nodeToGFM(tbodyEl);
return theadText + tbodyText;
},
......
......@@ -270,13 +270,23 @@ describe 'Copy as GFM', feature: true, js: true do
```
GFM
<<-GFM.strip_heredoc
<<-GFM.strip_heredoc,
```ruby
def foo
bar
end
```
GFM
<<-GFM.strip_heredoc
Foo
This is an example of GFM
```js
Code goes here
```
GFM
)
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