-
Notifications
You must be signed in to change notification settings - Fork 972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow inline summary cutoff #2581
base: next
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ use crate::codeblock::{CodeBlock, FenceSettings}; | |
use crate::shortcode::{Shortcode, SHORTCODE_PLACEHOLDER}; | ||
|
||
const CONTINUE_READING: &str = "<span id=\"continue-reading\"></span>"; | ||
const SUMMARY_CUTOFF_TEMPLATE: &str = "summary-cutoff.html"; | ||
const ANCHOR_LINK_TEMPLATE: &str = "anchor-link.html"; | ||
static EMOJI_REPLACER: Lazy<EmojiReplacer> = Lazy::new(EmojiReplacer::new); | ||
|
||
|
@@ -690,7 +691,9 @@ pub fn markdown_to_html( | |
event | ||
}); | ||
} | ||
Event::Html(text) if !has_summary && MORE_DIVIDER_RE.is_match(text.as_ref()) => { | ||
Event::Html(text) | Event::InlineHtml(text) | ||
if !has_summary && MORE_DIVIDER_RE.is_match(text.as_ref()) => | ||
{ | ||
has_summary = true; | ||
events.push(Event::Html(CONTINUE_READING.into())); | ||
} | ||
|
@@ -793,15 +796,50 @@ pub fn markdown_to_html( | |
.position(|e| matches!(e, Event::Html(CowStr::Borrowed(CONTINUE_READING)))) | ||
.unwrap_or(events.len()); | ||
|
||
// determine closing tags missing from summary | ||
let mut tags = Vec::new(); | ||
for event in &events[..continue_reading] { | ||
match event { | ||
Event::Start(Tag::HtmlBlock) | Event::End(TagEnd::HtmlBlock) => (), | ||
Event::Start(tag) => tags.push(tag.to_end()), | ||
Event::End(tag) => { | ||
tags.truncate(tags.iter().rposition(|t| *t == *tag).unwrap_or(0)); | ||
} | ||
_ => (), | ||
} | ||
} | ||
|
||
let mut events = events.into_iter(); | ||
|
||
// emit everything up to summary | ||
cmark::html::push_html(&mut html, events.by_ref().take(continue_reading)); | ||
|
||
if has_summary { | ||
// remove footnotes | ||
let summary_html = FOOTNOTES_RE.replace_all(&html, "").into_owned(); | ||
summary = Some(summary_html) | ||
let mut summary_html = FOOTNOTES_RE.replace_all(&html, "").into_owned(); | ||
|
||
// truncate trailing whitespace | ||
summary_html.truncate(summary_html.trim_end().len()); | ||
|
||
// add cutoff template | ||
if !tags.is_empty() { | ||
let mut c = tera::Context::new(); | ||
c.insert("summary", &summary_html); | ||
c.insert("lang", &context.lang); | ||
let summary_cutoff = utils::templates::render_template( | ||
SUMMARY_CUTOFF_TEMPLATE, | ||
&context.tera, | ||
c, | ||
&None, | ||
) | ||
.context("Failed to render summary cutoff template")?; | ||
summary_html.push_str(&summary_cutoff); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want a template or a string in the config? Template is more powerful but might be overkill? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like the template is ideal, although it's unlikely many people will change the template. The original idea was proposed as an easy option since even Without a template, you can't choose whether to emit an ellipsis based upon the closing punctuation, so, I'm inclined to say that the template is strictly better. |
||
} | ||
|
||
// close remaining tags | ||
cmark::html::push_html(&mut summary_html, tags.into_iter().rev().map(Event::End)); | ||
|
||
summary = Some(summary_html); | ||
} | ||
|
||
// emit everything after summary | ||
|
@@ -826,6 +864,7 @@ mod tests { | |
use super::*; | ||
use config::Config; | ||
use insta::assert_snapshot; | ||
use templates::ZOLA_TERA; | ||
|
||
#[test] | ||
fn insert_many_works() { | ||
|
@@ -881,7 +920,8 @@ mod tests { | |
let mores = | ||
["<!-- more -->", "<!--more-->", "<!-- MORE -->", "<!--MORE-->", "<!--\t MoRe \t-->"]; | ||
let config = Config::default(); | ||
let context = RenderContext::from_config(&config); | ||
let mut context = RenderContext::from_config(&config); | ||
context.tera.to_mut().extend(&ZOLA_TERA).unwrap(); | ||
for more in mores { | ||
let content = format!("{top}\n\n{more}\n\n{bottom}"); | ||
let rendered = markdown_to_html(&content, &context, vec![]).unwrap(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
source: components/markdown/tests/markdown.rs | ||
expression: summary | ||
--- | ||
<p>Hello (in en)</p> |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
source: components/markdown/tests/summary.rs | ||
expression: body | ||
--- | ||
<p>Things to do:</p> | ||
<ul> | ||
<li>Program… | ||
</li> | ||
</ul> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
… |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we error if the summary marker is inside a tag? Eg
<i <-- more --> class='' />
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since that's just straight-up invalid HTML, I'd be more inclined to just make the
<!-- more -->
regexp only match if it's by itself (i.e. not nested inside something like that). Will add a test for it.Personally, I think that an error would be weird; we don't emit errors for invalid HTML otherwise, so, it's better to just ignore it and treat it like otherwise malformed HTML.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I tried adding a test for this, but the markdown parser just isn't happy with it. If it's valid HTML, it parses, and if it's not, it just escapes it. So, in your example, it shows up as
<i
at the end of the summary.I'm inclined to just say that's okay, honestly. We could detect if it's inside any custom HTML tags, like
<i><!-- MORE --></i>
, but that goes against the principle that we aren't accounting for custom HTML tags at all and those just remain opened.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good