Improve handling of code blocks containing triple backticks

This commit is contained in:
Douwe Maan 2017-01-19 13:54:59 -06:00
parent d89f561612
commit bd2880bbc6
2 changed files with 30 additions and 4 deletions

View File

@ -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;
},

View File

@ -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