Add blockquote fence syntax to Markdown

This commit is contained in:
Douwe Maan 2016-04-27 23:38:33 +02:00
parent 92772f85c1
commit 6a477b9bfc
6 changed files with 345 additions and 2 deletions

View file

@ -7,11 +7,12 @@
* [Newlines](#newlines) * [Newlines](#newlines)
* [Multiple underscores in words](#multiple-underscores-in-words) * [Multiple underscores in words](#multiple-underscores-in-words)
* [URL auto-linking](#url-auto-linking) * [URL auto-linking](#url-auto-linking)
* [Multiline Blockquote](#multiline-blockquote)
* [Code and Syntax Highlighting](#code-and-syntax-highlighting) * [Code and Syntax Highlighting](#code-and-syntax-highlighting)
* [Inline Diff](#inline-diff) * [Inline Diff](#inline-diff)
* [Emoji](#emoji) * [Emoji](#emoji)
* [Special GitLab references](#special-gitlab-references) * [Special GitLab references](#special-gitlab-references)
* [Task lists](#task-lists) * [Task Lists](#task-lists)
**[Standard Markdown](#standard-markdown)** **[Standard Markdown](#standard-markdown)**
@ -89,6 +90,37 @@ GFM will autolink almost any URL you copy and paste into your text.
* irc://irc.freenode.net/gitlab * irc://irc.freenode.net/gitlab
* http://localhost:3000 * http://localhost:3000
## Multiline Blockquote
On top of standard Markdown [blockquotes](#blockquotes), which require prepending `>` to quoted lines,
GFM supports multiline blockquotes fenced by <code>>>></code>.
```no-highlight
>>>
If you paste a message from somewhere else
that
spans
multiple lines,
you can quote that without having to manually prepend `>` to every line!
>>>
```
>>>
If you paste a message from somewhere else
that
spans
multiple lines,
you can quote that without having to manually prepend `>` to every line!
>>>
## Code and Syntax Highlighting ## Code and Syntax Highlighting
_GitLab uses the [Rouge Ruby library][rouge] for syntax highlighting. For a _GitLab uses the [Rouge Ruby library][rouge] for syntax highlighting. For a

View file

@ -0,0 +1,50 @@
module Banzai
module Filter
class BlockquoteFenceFilter < HTML::Pipeline::TextFilter
REGEX = %r{
(?<code>
# Code blocks:
# ```
# Anything, including ignored `>>>` blocks
# ```
^```.+?\n```$
)
|
(?<html>
# HTML:
# <tag>
# Anything, including ignored `>>>` blocks
# </tag>
^<[^>]+?>.+?\n<\/[^>]+?>$
)
|
(
^>>>\n(?<quote>
(?:
(?!^```|^<[^>]+?>).
|
\g<code>
|
\g<html>
)
+?)\n>>>$
)
}mx.freeze
def initialize(text, context = nil, result = nil)
super text, context, result
@text = @text.delete "\r"
end
def call
@text.gsub(REGEX) do
if $~[:quote]
$~[:quote].gsub(/^/, "> ").gsub(/^> $/, ">")
else
$~[0]
end
end
end
end
end
end

View file

@ -3,7 +3,8 @@ module Banzai
class PreProcessPipeline < BasePipeline class PreProcessPipeline < BasePipeline
def self.filters def self.filters
FilterArray[ FilterArray[
Filter::YamlFrontMatterFilter Filter::YamlFrontMatterFilter,
Filter::BlockquoteFenceFilter,
] ]
end end

115
spec/fixtures/blockquote_fence_after.md vendored Normal file
View file

@ -0,0 +1,115 @@
Single `>>>` inside code block:
```
# Code
>>>
# Code
```
Double `>>>` inside code block:
```
# Code
>>>
# Code
>>>
# Code
```
Blockquote outside code block:
> Quote
Code block inside blockquote:
> Quote
>
> ```
> # Code
> ```
>
> Quote
Single `>>>` inside code block inside blockquote:
> Quote
>
> ```
> # Code
> >>>
> # Code
> ```
>
> Quote
Double `>>>` inside code block inside blockquote:
> Quote
>
> ```
> # Code
> >>>
> # Code
> >>>
> # Code
> ```
>
> Quote
Single `>>>` inside HTML:
<pre>
# Code
>>>
# Code
</pre>
Double `>>>` inside HTML:
<pre>
# Code
>>>
# Code
>>>
# Code
</pre>
Blockquote outside HTML:
> Quote
HTML inside blockquote:
> Quote
>
> <pre>
> # Code
> </pre>
>
> Quote
Single `>>>` inside HTML inside blockquote:
> Quote
>
> <pre>
> # Code
> >>>
> # Code
> </pre>
>
> Quote
Double `>>>` inside HTML inside blockquote:
> Quote
>
> <pre>
> # Code
> >>>
> # Code
> >>>
> # Code
> </pre>
>
> Quote

131
spec/fixtures/blockquote_fence_before.md vendored Normal file
View file

@ -0,0 +1,131 @@
Single `>>>` inside code block:
```
# Code
>>>
# Code
```
Double `>>>` inside code block:
```
# Code
>>>
# Code
>>>
# Code
```
Blockquote outside code block:
>>>
Quote
>>>
Code block inside blockquote:
>>>
Quote
```
# Code
```
Quote
>>>
Single `>>>` inside code block inside blockquote:
>>>
Quote
```
# Code
>>>
# Code
```
Quote
>>>
Double `>>>` inside code block inside blockquote:
>>>
Quote
```
# Code
>>>
# Code
>>>
# Code
```
Quote
>>>
Single `>>>` inside HTML:
<pre>
# Code
>>>
# Code
</pre>
Double `>>>` inside HTML:
<pre>
# Code
>>>
# Code
>>>
# Code
</pre>
Blockquote outside HTML:
>>>
Quote
>>>
HTML inside blockquote:
>>>
Quote
<pre>
# Code
</pre>
Quote
>>>
Single `>>>` inside HTML inside blockquote:
>>>
Quote
<pre>
# Code
>>>
# Code
</pre>
Quote
>>>
Double `>>>` inside HTML inside blockquote:
>>>
Quote
<pre>
# Code
>>>
# Code
>>>
# Code
</pre>
Quote
>>>

View file

@ -0,0 +1,14 @@
require 'rails_helper'
describe Banzai::Filter::BlockquoteFenceFilter, lib: true do
include FilterSpecHelper
it 'convers blockquote fences to blockquote lines' do
content = File.read(Rails.root.join('spec/fixtures/blockquote_fence_before.md'))
expected = File.read(Rails.root.join('spec/fixtures/blockquote_fence_after.md'))
output = filter(content)
expect(output).to eq(expected)
end
end