Commit d89f5616 authored by Douwe Maan's avatar Douwe Maan

Improve support for linebreaks, tables and nested blockquotes in lists

parent 2b37e4c1
...@@ -92,9 +92,6 @@ ...@@ -92,9 +92,6 @@
}, },
}, },
SanitizationFilter: { SanitizationFilter: {
'br'(el, text) {
return '<br>';
},
'dl'(el, text) { 'dl'(el, text) {
let lines = text.trim().split('\n'); let lines = text.trim().split('\n');
// Add two spaces to the front of subsequent list items lines, // Add two spaces to the front of subsequent list items lines,
...@@ -127,6 +124,10 @@ ...@@ -127,6 +124,10 @@
}, },
}, },
MarkdownFilter: { MarkdownFilter: {
'br'(el, text) {
// Two spaces at the end of a line are turned into a BR
return ' ';
},
'code'(el, text) { 'code'(el, text) {
let backtickCount = 1; let backtickCount = 1;
const backtickMatch = text.match(/`+/); const backtickMatch = text.match(/`+/);
...@@ -155,7 +156,7 @@ ...@@ -155,7 +156,7 @@
'li'(el, text) { 'li'(el, text) {
const lines = text.trim().split('\n'); const lines = text.trim().split('\n');
const firstLine = `- ${lines.shift()}`; const firstLine = `- ${lines.shift()}`;
// Add two spaces to the front of subsequent list items lines, // Add four spaces to the front of subsequent list items lines,
// or leave the line entirely blank. // or leave the line entirely blank.
const nextLines = lines.map((s) => { const nextLines = lines.map((s) => {
if (s.trim().length === 0) return ''; if (s.trim().length === 0) return '';
...@@ -213,7 +214,7 @@ ...@@ -213,7 +214,7 @@
}, },
'thead'(el, text) { 'thead'(el, text) {
const cells = _.map(el.querySelectorAll('th'), (cell) => { const cells = _.map(el.querySelectorAll('th'), (cell) => {
let chars = CopyAsGFM.nodeToGFM(cell).trim().length; let chars = CopyAsGFM.nodeToGFM(cell).trim().length + 2;
let before = ''; let before = '';
let after = ''; let after = '';
...@@ -231,14 +232,14 @@ ...@@ -231,14 +232,14 @@
break; break;
} }
chars = Math.max(chars, 0); chars = Math.max(chars, 3);
const middle = Array(chars + 1).join('-'); const middle = Array(chars + 1).join('-');
return before + middle + after; return before + middle + after;
}); });
return `${text}| ${cells.join(' | ')} |`; return `${text}|${cells.join('|')}|`;
}, },
'tr'(el, text) { 'tr'(el, text) {
const cells = _.map(el.querySelectorAll('td, th'), cell => CopyAsGFM.nodeToGFM(cell).trim()); const cells = _.map(el.querySelectorAll('td, th'), cell => CopyAsGFM.nodeToGFM(cell).trim());
......
...@@ -236,8 +236,6 @@ describe 'Copy as GFM', feature: true, js: true do ...@@ -236,8 +236,6 @@ describe 'Copy as GFM', feature: true, js: true do
it 'supports SanitizationFilter' do it 'supports SanitizationFilter' do
verify( verify(
<<-GFM.strip_heredoc <<-GFM.strip_heredoc
BR: <br>
<sub>sub</sub> <sub>sub</sub>
<dl> <dl>
...@@ -284,6 +282,8 @@ describe 'Copy as GFM', feature: true, js: true do ...@@ -284,6 +282,8 @@ describe 'Copy as GFM', feature: true, js: true do
it 'supports MarkdownFilter' do it 'supports MarkdownFilter' do
verify( verify(
"Line with two spaces at the end \nto insert a linebreak",
'`code`', '`code`',
'`` code with ` ticks ``', '`` code with ` ticks ``',
...@@ -319,6 +319,13 @@ describe 'Copy as GFM', feature: true, js: true do ...@@ -319,6 +319,13 @@ describe 'Copy as GFM', feature: true, js: true do
- Lists - Lists
GFM GFM
# list with blockquote
<<-GFM.strip_heredoc,
- List
> Blockquote
GFM
'1. Numbered list item', '1. Numbered list item',
# multiline numbered list item # multiline numbered list item
...@@ -355,10 +362,18 @@ describe 'Copy as GFM', feature: true, js: true do ...@@ -355,10 +362,18 @@ describe 'Copy as GFM', feature: true, js: true do
# table # table
<<-GFM.strip_heredoc, <<-GFM.strip_heredoc,
| Centered | Right | Left | | Centered | Right | Left |
| :------: | ----: | ---- | |:--------:|------:|------|
| Foo | Bar | **Baz** | | Foo | Bar | **Baz** |
| Foo | Bar | **Baz** | | Foo | Bar | **Baz** |
GFM GFM
# table with empty heading
<<-GFM.strip_heredoc,
| | x | y |
|---|---|---|
| a | 1 | 0 |
| b | 0 | 1 |
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