mirror of
https://github.com/cobalt-org/cobalt.rs
synced 2024-11-15 00:17:29 +00:00
Ref style links in excerpts (#188)
This commit is contained in:
parent
291229bffb
commit
24637e8867
16 changed files with 278 additions and 17 deletions
|
@ -297,6 +297,24 @@ impl Document {
|
|||
Ok(html.to_owned())
|
||||
}
|
||||
|
||||
/// Extracts references iff markdown content.
|
||||
pub fn extract_markdown_references(&self, excerpt_separator: &str) -> String {
|
||||
let mut trail = String::new();
|
||||
let re = Regex::new(r"(?m:^ {0,3}\[[^\]]+\]:.+$)").unwrap();
|
||||
|
||||
if self.markdown && re.is_match(&self.content) {
|
||||
for mat in re.find_iter(&self.content) {
|
||||
trail.push_str(&self.content[mat.0..mat.1]);
|
||||
trail.push('\n');
|
||||
}
|
||||
}
|
||||
trail +
|
||||
self.content
|
||||
.split(excerpt_separator)
|
||||
.next()
|
||||
.unwrap_or(&self.content)
|
||||
}
|
||||
|
||||
/// Renders excerpt and adds it to attributes of the document.
|
||||
pub fn render_excerpt(&mut self,
|
||||
context: &mut Context,
|
||||
|
@ -313,15 +331,15 @@ impl Document {
|
|||
.and_then(|attr| attr.as_str())
|
||||
.unwrap_or(default_excerpt_separator);
|
||||
|
||||
let excerpt = if let Some(excerpt_str) = excerpt_attr {
|
||||
excerpt_str
|
||||
if let Some(excerpt_str) = excerpt_attr {
|
||||
try!(self.render_html(excerpt_str, context, source))
|
||||
} else if excerpt_separator.is_empty() {
|
||||
""
|
||||
try!(self.render_html("", context, source))
|
||||
} else {
|
||||
self.content.split(excerpt_separator).next().unwrap_or(&self.content)
|
||||
};
|
||||
|
||||
try!(self.render_html(excerpt, context, source))
|
||||
try!(self.render_html(&self.extract_markdown_references(excerpt_separator),
|
||||
context,
|
||||
source))
|
||||
}
|
||||
};
|
||||
|
||||
self.attributes.insert("excerpt".to_owned(), Value::Str(excerpt_html));
|
||||
|
|
12
tests/fixtures/excerpts/posts/post-10.md
vendored
Normal file
12
tests/fixtures/excerpts/posts/post-10.md
vendored
Normal file
|
@ -0,0 +1,12 @@
|
|||
extends: default.liquid
|
||||
|
||||
title: Reference-style links immediately above first block
|
||||
date: 11 Feb 2017 04:50:21 -0000
|
||||
---
|
||||
|
||||
[20]: /0
|
||||
[21]: /1
|
||||
[22]: /2
|
||||
[23]: /3
|
||||
References are placed immediately above the first paragraph:
|
||||
[20][] [21][] [22][] [23][]
|
16
tests/fixtures/excerpts/posts/post-11.md
vendored
Normal file
16
tests/fixtures/excerpts/posts/post-11.md
vendored
Normal file
|
@ -0,0 +1,16 @@
|
|||
extends: default.liquid
|
||||
|
||||
title: Reference-style links immediately below first block do not resolve
|
||||
date: 11 Feb 2017 04:51:34 -0000
|
||||
---
|
||||
|
||||
References are placed immediately below the first paragraph:
|
||||
[30][] [31][] [32][] [33][]
|
||||
[30]: /0
|
||||
[31]: /1
|
||||
[32]: /2
|
||||
[33]: /3
|
||||
|
||||
Note that this is at it should be, as the markdown implementations I've tried
|
||||
(including CommonMark) do not render the above links (when the post is rendered
|
||||
individually).
|
14
tests/fixtures/excerpts/posts/post-12.liquid
vendored
Normal file
14
tests/fixtures/excerpts/posts/post-12.liquid
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
extends: default.liquid
|
||||
|
||||
title: Reference-style links in a non-markdown post do not resolve
|
||||
date: 11 Feb 2017 04:48:11 -0000
|
||||
---
|
||||
|
||||
References are placed below the first paragraph, and are separated from the
|
||||
first paragraph by a single blank line:
|
||||
[40][] [41][] [42][] [43][]
|
||||
|
||||
[40]: /0
|
||||
[41]: /1
|
||||
[42]: /2
|
||||
[43]: /3
|
2
tests/fixtures/excerpts/posts/post-6.liquid
vendored
2
tests/fixtures/excerpts/posts/post-6.liquid
vendored
|
@ -1,6 +1,6 @@
|
|||
extends: default.liquid
|
||||
|
||||
title: Implicit excepts work only in markdown
|
||||
title: Implicit excerpts work only in markdown
|
||||
date: 14 January 2016 21:05:30 -0500
|
||||
excerpt_separator: <!-- more -->
|
||||
---
|
||||
|
|
2
tests/fixtures/excerpts/posts/post-7.liquid
vendored
2
tests/fixtures/excerpts/posts/post-7.liquid
vendored
|
@ -1,6 +1,6 @@
|
|||
extends: default.liquid
|
||||
|
||||
title: Explicit excepts work even in liquid
|
||||
title: Explicit excerpts work even in liquid
|
||||
date: 14 January 2016 21:06:30 -0500
|
||||
excerpt: <strong>explicit</strong> excerpt
|
||||
excerpt_separator: <!-- more -->
|
||||
|
|
14
tests/fixtures/excerpts/posts/post-8.md
vendored
Normal file
14
tests/fixtures/excerpts/posts/post-8.md
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
extends: default.liquid
|
||||
|
||||
title: Reference-style links above first block and separated by blank line resolve
|
||||
date: 11 Feb 2017 04:45:01 -0000
|
||||
---
|
||||
|
||||
[00]: /0
|
||||
[01]: /1
|
||||
[02]: /2
|
||||
[03]: /3
|
||||
|
||||
References-style links are placed above the first paragraph, and are separated
|
||||
from the first paragraph by a single blank line:
|
||||
[00][] [01][] [02][] [03][]
|
14
tests/fixtures/excerpts/posts/post-9.md
vendored
Normal file
14
tests/fixtures/excerpts/posts/post-9.md
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
extends: default.liquid
|
||||
|
||||
title: Reference-style links below first block and separated by blank line resolve
|
||||
date: 11 Feb 2017 04:48:11 -0000
|
||||
---
|
||||
|
||||
References are placed below the first paragraph, and are separated from the
|
||||
first paragraph by a single blank line:
|
||||
[10][] [11][] [12][] [13][]
|
||||
|
||||
[10]: /0
|
||||
[11]: /1
|
||||
[12]: /2
|
||||
[13]: /3
|
|
@ -14,14 +14,57 @@
|
|||
<div>
|
||||
|
||||
<div>
|
||||
<h4>Explicit excepts work even in liquid</h4>
|
||||
<h4><a href="posts/post-7.html">Explicit excepts work even in liquid</a></h4>
|
||||
<h4>Reference-style links immediately below first block do not resolve</h4>
|
||||
<h4><a href="posts/post-11.html">Reference-style links immediately below first block do not resolve</a></h4>
|
||||
<p>References are placed immediately below the first paragraph:
|
||||
<a href="/0">30</a> <a href="/1">31</a> <a href="/2">32</a> <a href="/3">33</a>
|
||||
<a href="/0">30</a>: /0
|
||||
<a href="/1">31</a>: /1
|
||||
<a href="/2">32</a>: /2
|
||||
<a href="/3">33</a>: /3</p>
|
||||
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h4>Reference-style links immediately above first block</h4>
|
||||
<h4><a href="posts/post-10.html">Reference-style links immediately above first block</a></h4>
|
||||
<p>References are placed immediately above the first paragraph:
|
||||
<a href="/0">20</a> <a href="/1">21</a> <a href="/2">22</a> <a href="/3">23</a></p>
|
||||
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h4>Reference-style links in a non-markdown post do not resolve</h4>
|
||||
<h4><a href="posts/post-12.html">Reference-style links in a non-markdown post do not resolve</a></h4>
|
||||
References are placed below the first paragraph, and are separated from the
|
||||
first paragraph by a single blank line:
|
||||
[40][] [41][] [42][] [43][]
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h4>Reference-style links below first block and separated by blank line resolve</h4>
|
||||
<h4><a href="posts/post-9.html">Reference-style links below first block and separated by blank line resolve</a></h4>
|
||||
<p>References are placed below the first paragraph, and are separated from the
|
||||
first paragraph by a single blank line:
|
||||
<a href="/0">10</a> <a href="/1">11</a> <a href="/2">12</a> <a href="/3">13</a></p>
|
||||
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h4>Reference-style links above first block and separated by blank line resolve</h4>
|
||||
<h4><a href="posts/post-8.html">Reference-style links above first block and separated by blank line resolve</a></h4>
|
||||
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h4>Explicit excerpts work even in liquid</h4>
|
||||
<h4><a href="posts/post-7.html">Explicit excerpts work even in liquid</a></h4>
|
||||
<strong>explicit</strong> excerpt
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h4>Implicit excepts work only in markdown</h4>
|
||||
<h4><a href="posts/post-6.html">Implicit excepts work only in markdown</a></h4>
|
||||
<h4>Implicit excerpts work only in markdown</h4>
|
||||
<h4><a href="posts/post-6.html">Implicit excerpts work only in markdown</a></h4>
|
||||
<h1>Custom excerpt separator</h1>
|
||||
|
||||
<p>Welcome to the 6th post on cobalt.rs!</p>
|
||||
|
|
23
tests/target/excerpts/posts/post-10.html
Normal file
23
tests/target/excerpts/posts/post-10.html
Normal file
|
@ -0,0 +1,23 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
|
||||
<title>Reference-style links immediately above first block</title>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
|
||||
<div>
|
||||
<h2>Reference-style links immediately above first block</h2>
|
||||
<p>
|
||||
<p>References are placed immediately above the first paragraph:
|
||||
<a href="/0">20</a> <a href="/1">21</a> <a href="/2">22</a> <a href="/3">23</a></p>
|
||||
|
||||
</p>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
30
tests/target/excerpts/posts/post-11.html
Normal file
30
tests/target/excerpts/posts/post-11.html
Normal file
|
@ -0,0 +1,30 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
|
||||
<title>Reference-style links immediately below first block do not resolve</title>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
|
||||
<div>
|
||||
<h2>Reference-style links immediately below first block do not resolve</h2>
|
||||
<p>
|
||||
<p>References are placed immediately below the first paragraph:
|
||||
[30][] [31][] [32][] [33][]
|
||||
[30]: /0
|
||||
[31]: /1
|
||||
[32]: /2
|
||||
[33]: /3</p>
|
||||
<p>Note that this is at it should be, as the markdown implementations I've tried
|
||||
(including CommonMark) do not render the above links (when the post is rendered
|
||||
individually).</p>
|
||||
|
||||
</p>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
29
tests/target/excerpts/posts/post-12.html
Normal file
29
tests/target/excerpts/posts/post-12.html
Normal file
|
@ -0,0 +1,29 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
|
||||
<title>Reference-style links in a non-markdown post do not resolve</title>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
|
||||
<div>
|
||||
<h2>Reference-style links in a non-markdown post do not resolve</h2>
|
||||
<p>
|
||||
References are placed below the first paragraph, and are separated from the
|
||||
first paragraph by a single blank line:
|
||||
[40][] [41][] [42][] [43][]
|
||||
|
||||
[40]: /0
|
||||
[41]: /1
|
||||
[42]: /2
|
||||
[43]: /3
|
||||
|
||||
</p>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -2,14 +2,14 @@
|
|||
<html>
|
||||
<head>
|
||||
|
||||
<title>Implicit excepts work only in markdown</title>
|
||||
<title>Implicit excerpts work only in markdown</title>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
|
||||
<div>
|
||||
<h2>Implicit excepts work only in markdown</h2>
|
||||
<h2>Implicit excerpts work only in markdown</h2>
|
||||
<p>
|
||||
<h1>Custom excerpt separator</h1>
|
||||
|
||||
|
|
|
@ -2,14 +2,14 @@
|
|||
<html>
|
||||
<head>
|
||||
|
||||
<title>Explicit excepts work even in liquid</title>
|
||||
<title>Explicit excerpts work even in liquid</title>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
|
||||
<div>
|
||||
<h2>Explicit excepts work even in liquid</h2>
|
||||
<h2>Explicit excerpts work even in liquid</h2>
|
||||
<p>
|
||||
<h1>Custom excerpt separator</h1>
|
||||
|
||||
|
|
24
tests/target/excerpts/posts/post-8.html
Normal file
24
tests/target/excerpts/posts/post-8.html
Normal file
|
@ -0,0 +1,24 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
|
||||
<title>Reference-style links above first block and separated by blank line resolve</title>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
|
||||
<div>
|
||||
<h2>Reference-style links above first block and separated by blank line resolve</h2>
|
||||
<p>
|
||||
<p>References-style links are placed above the first paragraph, and are separated
|
||||
from the first paragraph by a single blank line:
|
||||
<a href="/0">00</a> <a href="/1">01</a> <a href="/2">02</a> <a href="/3">03</a></p>
|
||||
|
||||
</p>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
24
tests/target/excerpts/posts/post-9.html
Normal file
24
tests/target/excerpts/posts/post-9.html
Normal file
|
@ -0,0 +1,24 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
|
||||
<title>Reference-style links below first block and separated by blank line resolve</title>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
|
||||
<div>
|
||||
<h2>Reference-style links below first block and separated by blank line resolve</h2>
|
||||
<p>
|
||||
<p>References are placed below the first paragraph, and are separated from the
|
||||
first paragraph by a single blank line:
|
||||
<a href="/0">10</a> <a href="/1">11</a> <a href="/2">12</a> <a href="/3">13</a></p>
|
||||
|
||||
</p>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue