Strip footnotes link from summaries

Closes #1282
This commit is contained in:
Vincent Prouillet 2021-05-08 23:27:03 +02:00
parent b2adfae4bc
commit fecc3cf148
3 changed files with 46 additions and 8 deletions

View file

@ -1,6 +1,6 @@
# Changelog
## unreleased
## 0.14.0 (unreleased)
### Breaking
@ -24,7 +24,9 @@
- Add `path` to the taxonomy terms to be on par with pages and sections
- Add the `base16-aterlierdune-light` syntax highlight theme
- Improve link checking: less concurrency and try to not overload the servers
- Allow using POST for `load_data`, along with a body to POST
- Allow using POST for `load_data`, along with a body to POST and allow it to fail
- Add Zig syntax highlighting
- Footnotes links are now stripped from summaries - they were not linking to anything.
## 0.13.0 (2021-01-09)

View file

@ -12,11 +12,14 @@ pub use page::PageFrontMatter;
pub use section::SectionFrontMatter;
lazy_static! {
static ref TOML_RE: Regex =
Regex::new(r"^[[:space:]]*\+\+\+(\r?\n(?s).*?(?-s))\+\+\+[[:space:]]*(?:$|(?:\r?\n((?s).*(?-s))$))")
.unwrap();
static ref YAML_RE: Regex =
Regex::new(r"^[[:space:]]*---(\r?\n(?s).*?(?-s))---[[:space:]]*(?:$|(?:\r?\n((?s).*(?-s))$))").unwrap();
static ref TOML_RE: Regex = Regex::new(
r"^[[:space:]]*\+\+\+(\r?\n(?s).*?(?-s))\+\+\+[[:space:]]*(?:$|(?:\r?\n((?s).*(?-s))$))"
)
.unwrap();
static ref YAML_RE: Regex = Regex::new(
r"^[[:space:]]*---(\r?\n(?s).*?(?-s))---[[:space:]]*(?:$|(?:\r?\n((?s).*(?-s))$))"
)
.unwrap();
}
pub enum RawFrontMatter<'a> {

View file

@ -27,6 +27,8 @@ lazy_static! {
static ref RFC3339_DATE: Regex = Regex::new(
r"^(?P<datetime>(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])(T([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(Z|(\+|-)([01][0-9]|2[0-3]):([0-5][0-9])))?)\s?(_|-)(?P<slug>.+$)"
).unwrap();
static ref FOOTNOTES_RE: Regex = Regex::new(r"<sup\s*.*?>\s*.*?</sup>").unwrap();
}
#[derive(Clone, Debug, Default, PartialEq)]
@ -263,7 +265,11 @@ impl Page {
Error::chain(format!("Failed to render content of {}", self.file.path.display()), e)
})?;
self.summary = res.summary_len.map(|l| res.body[0..l].to_owned());
self.summary = if let Some(s) = res.summary_len.map(|l| &res.body[0..l]) {
Some(FOOTNOTES_RE.replace(s, "").into_owned())
} else {
None
};
self.content = res.body;
self.toc = res.toc;
self.external_links = res.external_links;
@ -530,6 +536,33 @@ Hello world
assert_eq!(page.summary, Some("<p>Hello world</p>\n".to_string()));
}
#[test]
fn strips_footnotes_in_summary() {
let config = Config::default();
let content = r#"
+++
+++
This page has footnotes, here's one. [^1]
<!-- more -->
And here's another. [^2]
[^1]: This is the first footnote.
[^2]: This is the second footnote."#
.to_string();
let res = Page::parse(Path::new("hello.md"), &content, &config, &PathBuf::new());
assert!(res.is_ok());
let mut page = res.unwrap();
page.render_markdown(&HashMap::default(), &Tera::default(), &config, InsertAnchor::None)
.unwrap();
assert_eq!(
page.summary,
Some("<p>This page has footnotes, here\'s one. </p>\n".to_string())
);
}
#[test]
fn page_with_assets_gets_right_info() {
let tmp_dir = tempdir().expect("create temp dir");