use std::collections::BTreeSet;
use std::fmt;
use std::ops::Range;
use pulldown_cmark::{
Alignment, CodeBlockKind, Event, HeadingLevel, LinkType, Options, Parser, Tag, TagEnd,
};
pub const OPTIONS: Options = Options::ENABLE_TABLES;
pub const MAX_TITLE_CHARS: usize = 80;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TrustedHtml(String);
impl TrustedHtml {
pub fn as_str(&self) -> &str {
&self.0
}
pub fn json_ld(data: &serde_json::Value) -> Self {
let json = data
.to_string()
.replace('<', "\\u003c")
.replace('>', "\\u003e")
.replace('&', "\\u0026");
Self(format!(
"<script type=\"application/ld+json\">{json}</script>"
))
}
}
impl fmt::Display for TrustedHtml {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl askama::filters::HtmlSafe for TrustedHtml {}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Counting {
Words,
Characters,
}
#[derive(Debug)]
pub struct Article {
pub title: String,
pub body: TrustedHtml,
pub words: usize,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Outline {
pub summary: String,
pub sections: Vec<(String, String)>,
}
pub fn outline(text: &str) -> Outline {
enum Target {
None,
Heading,
Paragraph,
}
let mut outline = Outline::default();
let (mut target, mut buffer) = (Target::None, String::new());
let mut skipped = 0_usize;
let mut seen_title = false;
let mut open_section = false;
for event in Parser::new_ext(text, OPTIONS) {
match event {
Event::Start(
Tag::CodeBlock(_)
| Tag::HtmlBlock
| Tag::List(_)
| Tag::Table(_)
| Tag::BlockQuote(_),
) => skipped += 1,
Event::End(
TagEnd::CodeBlock
| TagEnd::HtmlBlock
| TagEnd::List(_)
| TagEnd::Table
| TagEnd::BlockQuote(_),
) => skipped = skipped.saturating_sub(1),
_ if skipped > 0 => {}
Event::Start(Tag::Heading { level, .. }) => {
buffer.clear();
open_section = false;
target = if level == HeadingLevel::H2 {
Target::Heading
} else {
Target::None
};
}
Event::End(TagEnd::Heading(level)) => {
if level == HeadingLevel::H1 {
seen_title = true;
}
if matches!(target, Target::Heading) {
outline.sections.push((tidy(&buffer), String::new()));
open_section = true;
}
target = Target::None;
}
Event::Start(Tag::Paragraph) if seen_title => {
buffer.clear();
target = Target::Paragraph;
}
Event::End(TagEnd::Paragraph) if matches!(target, Target::Paragraph) => {
let paragraph = tidy(&buffer);
if outline.summary.is_empty() {
outline.summary = paragraph.clone();
}
if open_section && let Some(section) = outline.sections.last_mut() {
section.1 = paragraph;
open_section = false;
}
target = Target::None;
}
Event::Text(text) | Event::Code(text) | Event::InlineHtml(text) => {
if !matches!(target, Target::None) {
buffer.push_str(&text);
}
}
Event::SoftBreak | Event::HardBreak => buffer.push(' '),
_ => {}
}
}
outline
}
fn tidy(text: &str) -> String {
text.split_whitespace().collect::<Vec<_>>().join(" ")
}
pub fn shorten(text: &str, limit: usize) -> String {
if text.chars().count() <= limit {
return text.to_owned();
}
let kept: String = text.chars().take(limit.saturating_sub(1)).collect();
let cut = match kept.rfind(' ') {
Some(space) if kept[space..].chars().count() <= 24 => &kept[..space],
_ => kept.as_str(),
};
format!("{}…", cut.trim_end_matches([',', ';', ':', ' ', '、']))
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Problem {
pub offset: usize,
pub reason: String,
}
pub fn render(
text: &str,
routes: &BTreeSet<String>,
counting: Counting,
) -> Result<Article, Problem> {
let mut events = Parser::new_ext(text, OPTIONS).into_offset_iter();
let title = title(&mut events)?;
let mut policy = Policy::new(routes, counting);
let mut allowed = Vec::new();
for (event, range) in events {
let event = policy.event(event).map_err(|reason| Problem {
offset: range.start,
reason,
})?;
allowed.push(event);
}
let mut html = String::new();
pulldown_cmark::html::push_html(&mut html, allowed.into_iter());
Ok(Article {
title,
body: TrustedHtml(html),
words: policy.words + policy.characters / 2,
})
}
fn title<'a>(
events: &mut impl Iterator<Item = (Event<'a>, Range<usize>)>,
) -> Result<String, Problem> {
let problem = |range: &Range<usize>, reason: &str| Problem {
offset: range.start,
reason: reason.to_owned(),
};
let heading = match events.next() {
Some((
Event::Start(Tag::Heading {
level: HeadingLevel::H1,
..
}),
range,
)) => range,
other => {
let range = other.map_or(0..0, |(_, range)| range);
return Err(problem(
&range,
"the first block must be a level-1 heading, such as `# My note`",
));
}
};
let mut title = String::new();
for (event, range) in events.by_ref() {
match event {
Event::End(TagEnd::Heading(_)) => break,
Event::Text(text) | Event::Code(text) | Event::InlineHtml(text) => {
title.push_str(&text);
}
Event::SoftBreak | Event::HardBreak => title.push(' '),
Event::Start(Tag::Emphasis | Tag::Strong)
| Event::End(TagEnd::Emphasis | TagEnd::Strong) => {}
Event::Start(Tag::Link { .. } | Tag::Image { .. }) => {
return Err(problem(
&range,
"the title cannot contain a link or an image",
));
}
_ => {
return Err(problem(
&range,
"the title can contain only text, emphasis and inline code",
));
}
}
}
let title = title.trim();
match title.chars().count() {
0 => Err(problem(&heading, "the title is empty")),
n if n > MAX_TITLE_CHARS => {
Err(problem(&heading, "the title is longer than 80 characters"))
}
_ => Ok(title.to_owned()),
}
}
struct Policy<'r> {
routes: &'r BTreeSet<String>,
counting: Counting,
code_depth: usize,
words: usize,
characters: usize,
in_word: bool,
}
impl<'r> Policy<'r> {
fn new(routes: &'r BTreeSet<String>, counting: Counting) -> Self {
Self {
routes,
counting,
code_depth: 0,
words: 0,
characters: 0,
in_word: false,
}
}
fn event<'a>(&mut self, event: Event<'a>) -> Result<Event<'a>, String> {
match &event {
Event::Start(Tag::Emphasis | Tag::Strong | Tag::Link { .. })
| Event::End(TagEnd::Emphasis | TagEnd::Strong | TagEnd::Link) => {}
Event::Start(_) | Event::End(_) | Event::SoftBreak | Event::HardBreak | Event::Rule => {
self.in_word = false;
}
_ => {}
}
Ok(match event {
Event::Start(tag) => Event::Start(self.start(tag)?),
Event::End(tag) => Event::End(self.end(tag)?),
Event::Text(text) => {
self.count(&text);
Event::Text(text)
}
Event::Code(code) => {
if self.counting == Counting::Characters || !self.in_word {
self.words += 1;
}
self.in_word = true;
Event::Code(code)
}
Event::Html(html) | Event::InlineHtml(html) => {
self.count(&html);
Event::Text(html)
}
Event::SoftBreak => Event::SoftBreak,
Event::HardBreak => Event::HardBreak,
Event::Rule => Event::Rule,
Event::InlineMath(_) | Event::DisplayMath(_) => return Err(unsupported("math")),
Event::FootnoteReference(_) => return Err(unsupported("a footnote")),
Event::TaskListMarker(_) => return Err(unsupported("a task list")),
})
}
fn start<'a>(&mut self, tag: Tag<'a>) -> Result<Tag<'a>, String> {
Ok(match tag {
Tag::Paragraph => Tag::Paragraph,
Tag::Heading {
level: HeadingLevel::H1,
..
} => {
return Err(
"only the title can be a level-1 heading; use `## ` for a section".into(),
);
}
Tag::Heading { level, .. } => Tag::Heading {
level,
id: None,
classes: Vec::new(),
attrs: Vec::new(),
},
Tag::BlockQuote(None) => Tag::BlockQuote(None),
Tag::CodeBlock(_) | Tag::HtmlBlock => {
self.code_depth += 1;
Tag::CodeBlock(CodeBlockKind::Indented)
}
Tag::List(start) => Tag::List(start),
Tag::Item => Tag::Item,
Tag::Table(columns) => Tag::Table(vec![Alignment::None; columns.len()]),
Tag::TableHead => Tag::TableHead,
Tag::TableRow => Tag::TableRow,
Tag::TableCell => Tag::TableCell,
Tag::Emphasis => Tag::Emphasis,
Tag::Strong => Tag::Strong,
Tag::Link {
link_type,
dest_url,
title,
id,
} => {
let checked = matches!(
link_type,
LinkType::Inline
| LinkType::Reference
| LinkType::Collapsed
| LinkType::Shortcut
| LinkType::Autolink
);
if !(checked
&& (self.routes.contains(dest_url.as_ref()) || is_https_address(&dest_url)))
{
let shown = match link_type {
LinkType::Email => format!("mailto:{dest_url}"),
_ => dest_url.to_string(),
};
return Err(format!(
"the link {shown:?} is neither a page of this site, such as \"/\", nor an https:// address"
));
}
Tag::Link {
link_type,
dest_url,
title,
id,
}
}
Tag::Image { .. } => return Err(unsupported("an image")),
Tag::BlockQuote(Some(_)) => return Err(unsupported("an alert block quote")),
Tag::FootnoteDefinition(_) => return Err(unsupported("a footnote")),
Tag::DefinitionList | Tag::DefinitionListTitle | Tag::DefinitionListDefinition => {
return Err(unsupported("a definition list"));
}
Tag::Strikethrough => return Err(unsupported("strikethrough")),
Tag::Superscript | Tag::Subscript => {
return Err(unsupported("superscript or subscript"));
}
Tag::MetadataBlock(_) => return Err(unsupported("a metadata block")),
})
}
fn end(&mut self, tag: TagEnd) -> Result<TagEnd, String> {
Ok(match tag {
TagEnd::CodeBlock | TagEnd::HtmlBlock => {
self.code_depth = self.code_depth.saturating_sub(1);
TagEnd::CodeBlock
}
TagEnd::Paragraph
| TagEnd::Heading(_)
| TagEnd::BlockQuote(None)
| TagEnd::List(_)
| TagEnd::Item
| TagEnd::Table
| TagEnd::TableHead
| TagEnd::TableRow
| TagEnd::TableCell
| TagEnd::Emphasis
| TagEnd::Strong
| TagEnd::Link => tag,
TagEnd::BlockQuote(Some(_))
| TagEnd::Image
| TagEnd::FootnoteDefinition
| TagEnd::DefinitionList
| TagEnd::DefinitionListTitle
| TagEnd::DefinitionListDefinition
| TagEnd::Strikethrough
| TagEnd::Superscript
| TagEnd::Subscript
| TagEnd::MetadataBlock(_) => return Err(unsupported("this Markdown")),
})
}
fn count(&mut self, text: &str) {
if self.code_depth > 0 {
return;
}
match self.counting {
Counting::Words => {
for c in text.chars() {
if c.is_whitespace() {
self.in_word = false;
} else if !self.in_word {
self.words += 1;
self.in_word = true;
}
}
}
Counting::Characters => {
self.characters += text.chars().filter(|c| !c.is_whitespace()).count();
}
}
}
}
fn unsupported(what: &str) -> String {
format!("{what} is not supported in articles")
}
pub fn is_https_address(dest: &str) -> bool {
let Some(rest) = dest.strip_prefix("https://") else {
return false;
};
let (host, tail) = rest.split_at(rest.find(['/', '?', '#']).unwrap_or(rest.len()));
let host_ok = (1..=253).contains(&host.len())
&& host
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'.' || b == b'-')
&& host.contains('.')
&& !host.starts_with(['.', '-'])
&& !host.ends_with(['.', '-']);
let tail_ok = tail
.bytes()
.all(|b| (b'!'..=b'~').contains(&b) && !b"\"<>\\^`{|}".contains(&b));
host_ok && tail_ok
}
#[cfg(test)]
mod tests {
use super::*;
use pulldown_cmark::{BlockQuoteKind, CowStr};
const ALLOWED_TAGS: [&str; 23] = [
"p",
"h2",
"h3",
"h4",
"h5",
"h6",
"blockquote",
"ul",
"ol",
"li",
"table",
"thead",
"tbody",
"tr",
"th",
"td",
"em",
"strong",
"code",
"pre",
"a",
"br",
"hr",
];
fn routes() -> BTreeSet<String> {
["/", "/ja/", "/first-note", "/ja/first-note"]
.into_iter()
.map(str::to_owned)
.collect()
}
fn rendered(text: &str) -> Result<Article, Problem> {
render(text, &routes(), Counting::Words)
}
fn body(markdown: &str) -> String {
let text = format!("# T\n\n{markdown}");
match rendered(&text) {
Ok(article) => article.body.as_str().to_owned(),
Err(problem) => panic!("{markdown:?} was refused: {problem:?}"),
}
}
fn refusal(text: &str) -> (usize, String) {
let problem = rendered(text).expect_err("refused");
let line = text[..problem.offset].matches('\n').count() + 1;
(line, problem.reason)
}
fn tags(html: &str) -> Vec<(String, Vec<String>)> {
let mut found = Vec::new();
let mut rest = html;
while let Some(start) = rest.find('<') {
let end = start + rest[start..].find('>').expect("a closed tag");
let inner = rest[start + 1..end]
.trim_start_matches('/')
.trim_end_matches('/')
.trim_end();
let name_end = inner.find(char::is_whitespace).unwrap_or(inner.len());
let mut attributes = Vec::new();
let mut attrs = inner[name_end..].trim_start();
while !attrs.is_empty() {
let name_end = attrs.find(['=', ' ']).unwrap_or(attrs.len());
attributes.push(attrs[..name_end].to_owned());
attrs = &attrs[name_end..];
if let Some(value) = attrs.strip_prefix("=\"") {
let close = value.find('"').expect("a quoted value");
attrs = &value[close + 1..];
}
attrs = attrs.trim_start();
}
found.push((inner[..name_end].to_owned(), attributes));
rest = &rest[end + 1..];
}
found
}
fn assert_only_allowed_markup(html: &str) {
for (tag, attributes) in tags(html) {
assert!(ALLOWED_TAGS.contains(&tag.as_str()), "<{tag}> in {html}");
for attribute in attributes {
assert!(
["href", "title", "start"].contains(&attribute.as_str()),
"{attribute} on <{tag}> in {html}"
);
}
}
}
#[test]
fn options_enable_tables_and_nothing_else() {
assert_eq!(OPTIONS, Options::ENABLE_TABLES);
}
#[test]
fn ordinary_markdown_becomes_standard_html() {
let html = body(
"Some *emphasis*, **strong** and `code`.\nNext line. \nBreak.\n\n\
## Section\n\n###### Small\n\n> Quoted\n\n- one\n- two\n\n3. three\n4. four\n\n\
| a | b |\n|:--|--:|\n| 1 | 2 |\n\n---\n",
);
assert_eq!(
html,
"<p>Some <em>emphasis</em>, <strong>strong</strong> and <code>code</code>.\nNext line.<br />\nBreak.</p>\n\
<h2>Section</h2>\n<h6>Small</h6>\n<blockquote>\n<p>Quoted</p>\n</blockquote>\n\
<ul>\n<li>one</li>\n<li>two</li>\n</ul>\n<ol start=\"3\">\n<li>three</li>\n<li>four</li>\n</ol>\n\
<table><thead><tr><th>a</th><th>b</th></tr></thead><tbody>\n<tr><td>1</td><td>2</td></tr>\n</tbody></table>\n<hr />\n"
);
}
#[test]
fn markup_in_the_source_is_shown_as_text() {
let cases = [
"<script>alert(1)</script>",
"<img src=x onerror=alert(1)>",
"<div onclick=\"x()\">block</div>\n",
"Inline <b onmouseover=x>bold</b> and <!-- comment -->",
"`<script>in code</script>`",
"```\n<script>fenced</script>\n```",
" <script>indented</script>",
];
for markdown in cases {
let html = body(markdown);
assert_only_allowed_markup(&html);
assert!(!html.contains("<script"), "{html}");
assert!(html.contains("<"), "{markdown:?} lost its text: {html}");
}
let html = body("<img src=x onerror=alert(1)>");
assert_eq!(
html,
"<pre><code><img src=x onerror=alert(1)></code></pre>\n"
);
}
#[test]
fn the_info_string_of_a_code_block_is_dropped() {
let html = body(
"```\"><script>alert(1)</script>\nlet x = 1;\n```\n\n```rust file=src/x.rs\nfn f() {}\n```",
);
assert_eq!(
html,
"<pre><code>let x = 1;\n</code></pre>\n<pre><code>fn f() {}\n</code></pre>\n"
);
}
#[test]
fn images_and_a_second_title_are_refused() {
assert_eq!(
refusal("# T\n\n\n"),
(3, "an image is not supported in articles".into())
);
let second = "only the title can be a level-1 heading; use `## ` for a section";
assert_eq!(refusal("# T\n\ntext\n\n# Again\n"), (5, second.into()));
assert_eq!(refusal("# T\n\nAgain\n=====\n"), (3, second.into()));
}
#[test]
fn disabled_extensions_stay_literal_text() {
let html = body(
"Note[^1] and $x^2$ and ~~gone~~ and ^up^ and ~down~ and [[Wiki]].\n\n\
[^1]: The footnote.\n\n- [ ] open\n- [x] done\n\n> [!NOTE]\n> Alert.\n\n\
Term\n: Definition\n\n---\ntitle: x\n---\n",
);
assert_only_allowed_markup(&html);
for absent in ["<input", "<sup", "<math", "<del", "<dl", "class="] {
assert!(!html.contains(absent), "{absent} in {html}");
}
for literal in [
"[^1]",
"$x^2$",
"~~gone~~",
"[ ] open",
"[x] done",
"[!NOTE]",
": Definition",
] {
assert!(html.contains(literal), "{literal} missing from {html}");
}
}
#[test]
fn unsupported_events_fail_closed() {
let routes = routes();
let events = [
Event::FootnoteReference(CowStr::from("1")),
Event::TaskListMarker(false),
Event::InlineMath(CowStr::from("x")),
Event::DisplayMath(CowStr::from("x")),
Event::Start(Tag::FootnoteDefinition(CowStr::from("1"))),
Event::Start(Tag::Strikethrough),
Event::Start(Tag::Superscript),
Event::Start(Tag::DefinitionList),
Event::Start(Tag::BlockQuote(Some(BlockQuoteKind::Note))),
Event::Start(Tag::Image {
link_type: LinkType::Inline,
dest_url: CowStr::from("https://example.com/a.png"),
title: CowStr::from(""),
id: CowStr::from(""),
}),
Event::Start(Tag::Link {
link_type: LinkType::WikiLink { has_pothole: false },
dest_url: CowStr::from("/first-note"),
title: CowStr::from(""),
id: CowStr::from(""),
}),
Event::End(TagEnd::Strikethrough),
];
for event in events {
let shown = format!("{event:?}");
assert!(
Policy::new(&routes, Counting::Words).event(event).is_err(),
"{shown} was accepted"
);
}
}
#[test]
fn link_titles_are_kept_escaped() {
let html = body("[x](/first-note \"a \\\" onmouseover=alert(1) <b>\")");
assert_eq!(
html,
"<p><a href=\"/first-note\" title=\"a " onmouseover=alert(1) <b>\">x</a></p>\n"
);
assert_only_allowed_markup(&html);
}
#[test]
fn allowed_link_destinations_pass() {
for dest in [
"/",
"/ja/",
"/first-note",
"/ja/first-note",
"https://example.com",
"https://example.com/",
"https://doc.rust-lang.org/book/ch01-01-installation.html",
"https://a-b.example.org/x?y=1&z=%20#top",
"https://example.com#top",
] {
let html = body(&format!("[x](<{dest}>)"));
assert!(html.contains("<a href=\""), "{dest}: {html}");
}
let html = body("<https://example.com/a>");
assert_eq!(
html,
"<p><a href=\"https://example.com/a\">https://example.com/a</a></p>\n"
);
}
#[test]
fn other_link_destinations_are_refused_with_line_and_quoted_text() {
let cases = [
("[x](javascript:alert(1))", "javascript:alert(1)"),
("[x](JavaScript:x)", "JavaScript:x"),
("[x](javascript:x)", "javascript:x"),
("[x](data:text/html,x)", "data:text/html,x"),
("[x](file:///etc/passwd)", "file:///etc/passwd"),
("[x](http://example.com)", "http://example.com"),
("[x](//example.com)", "//example.com"),
("[x](</\\example.com>)", "/\\example.com"),
("[x](https:example.com)", "https:example.com"),
("[x](https://user@example.com)", "https://user@example.com"),
("[x](https://example.com:8443)", "https://example.com:8443"),
("[x](https://%65xample.com)", "https://%65xample.com"),
("[x](/first%2Dnote)", "/first%2Dnote"),
("[x](/first-note?x=1)", "/first-note?x=1"),
("[x](/first-note#top)", "/first-note#top"),
("[x](#top)", "#top"),
("[x](/../first-note)", "/../first-note"),
("[x](/./first-note)", "/./first-note"),
("[x](/ja/../x)", "/ja/../x"),
("[x](first-note)", "first-note"),
("[x](./first-note)", "./first-note"),
("[x](/first-note/)", "/first-note/"),
("[x](/ja)", "/ja"),
("[x](/missing)", "/missing"),
("[x]()", ""),
("[x](<https://example.com/a b>)", "https://example.com/a b"),
(
"[x](<https://example.com/a\tb>)",
"https://example.com/a\tb",
),
(
"[x](<https://example.com/\u{1}>)",
"https://example.com/\u{1}",
),
(
"[x](<https://example.com/\u{1f}>)",
"https://example.com/\u{1f}",
),
(
"[x](<https://example.com/\u{7f}>)",
"https://example.com/\u{7f}",
),
("[x](<https://example.com/\\x>)", "https://example.com/\\x"),
("[x](<https://EXAMPLE.com>)", "https://EXAMPLE.com"),
(
"[x][ref]\n\n[ref]: mailto:a@example.com",
"mailto:a@example.com",
),
("<a@example.com>", "mailto:a@example.com"),
];
for (markdown, dest) in cases {
let (line, reason) = refusal(&format!("# T\n\ntext\n{markdown}\n"));
assert_eq!(line, 4, "{markdown:?}");
let quoted = format!("the link {dest:?} is neither");
assert!(reason.starts_with("ed), "{markdown:?}: {reason}");
assert!(!reason.chars().any(char::is_control), "{reason:?}");
}
}
#[test]
fn destinations_the_parser_cannot_link_are_still_refused_by_the_rule() {
for dest in [
"https://example.com/\n",
"https://example.com/\r",
"https://example.com/\0",
"https://exa_mple.com",
"https://.example.com",
"https://example.com.",
"https://-example.com",
"https://localhost",
"https://example.com/{x}",
"https://example.com/|",
"https://example.com/^",
"https://example.com/`",
"https://example.com/\"",
"https://example.com/<",
"https://example.com/é",
"",
] {
assert!(!is_https_address(dest), "{dest:?}");
}
let longest = format!("https://{}.com", "a".repeat(249));
assert!(is_https_address(&longest));
assert!(!is_https_address(&format!(
"https://{}.com",
"a".repeat(250)
)));
}
#[test]
fn the_title_is_the_opening_level_1_heading() {
let article = rendered("# Field *notes* with `code` and <b>tags</b>\n\nBody.\n").unwrap();
assert_eq!(article.title, "Field notes with code and <b>tags</b>");
assert_eq!(article.body.as_str(), "<p>Body.</p>\n");
let setext = rendered("Setext title\n============\n").unwrap();
assert_eq!(setext.title, "Setext title");
assert_eq!(
rendered(&format!("# {}\n", "é".repeat(80)))
.unwrap()
.title
.chars()
.count(),
80
);
}
#[test]
fn title_rules_are_enforced_with_a_line() {
let first = "the first block must be a level-1 heading, such as `# My note`";
assert_eq!(refusal(""), (1, first.into()));
assert_eq!(refusal("Text first.\n\n# Late\n"), (1, first.into()));
assert_eq!(refusal("\n\n## Section\n\n# Late\n"), (3, first.into()));
assert_eq!(refusal("#\n\nBody\n"), (1, "the title is empty".into()));
assert_eq!(refusal("# \u{3000} \n"), (1, "the title is empty".into()));
assert_eq!(
refusal(&format!("\n# {}\n", "x".repeat(81))),
(2, "the title is longer than 80 characters".into())
);
let link = "the title cannot contain a link or an image";
assert_eq!(refusal("# A [link](/first-note)\n"), (1, link.into()));
assert_eq!(
refusal("# A \n"),
(1, link.into())
);
assert_eq!(refusal("# A <https://example.com>\n"), (1, link.into()));
}
#[test]
fn english_counts_words_outside_code() {
let article = rendered(
"# Not counted in the title\n\nOne two three\nfour `five six` seven.\n\n```\nnot counted at all\n```\n\n<p>\nhtml block\n</p>\n",
)
.unwrap();
assert_eq!(article.words, 6);
}
#[test]
fn formatting_inside_a_word_does_not_split_it() {
let text = format!("# T\n\n{}\n", "foo**bar**baz ".repeat(100));
let words = rendered(&text).unwrap().words;
assert_eq!(words, 100);
assert_eq!(crate::reading::reading_minutes(words), 1);
}
#[test]
fn whitespace_breaks_and_blocks_separate_words_but_inline_markup_does_not() {
let words = |markdown: &str| rendered(&format!("# T\n\n{markdown}")).unwrap().words;
assert_eq!(words("*a*b [c](/)d e`f`g h<b>i</b>j\n"), 4);
assert_eq!(words("[one two](/first-note), three\n"), 3);
assert_eq!(words("a *b* [c](/) `d` <b>e</b>\n"), 5);
assert_eq!(words("a\nb \nc\n"), 3);
assert_eq!(words("a\n\nb\n\n## c\n\n- d\n- e\n\n> f\n"), 6);
assert_eq!(words("| a | b |\n|---|---|\n| c | d |\n"), 4);
assert_eq!(words("a\n\n```\nnot counted\n```\nb\n"), 2);
}
#[test]
fn japanese_counts_two_characters_as_a_word() {
let text = "# 題名\n\nこれは 日本語の文です。\n\n`code` と、\n\n```\n数えない\n```\n";
let article = render(text, &routes(), Counting::Characters).unwrap();
assert_eq!(article.words, 13 / 2 + 1);
}
#[test]
fn the_outline_holds_the_first_paragraph_and_each_h2_with_its_paragraph() {
let text = "# Title with `code`\n\n\
First *paragraph* with [a link](https://example.com/) and `code`\nover two lines.\n\n\
Second paragraph.\n\n\
## What is it?\n\n```sh\nnot this\n```\n\n- nor this\n\n\
The **answer** <b>plain</b>.\n\nMore.\n\n\
### Deeper\n\nSkipped for sections.\n\n\
## No paragraph\n\n## Last one\n\nEnd.\n";
let outline = outline(text);
assert_eq!(
outline.summary,
"First paragraph with a link and code over two lines."
);
assert_eq!(
outline.sections,
vec![
(
"What is it?".to_owned(),
"The answer <b>plain</b>.".to_owned()
),
("No paragraph".to_owned(), String::new()),
("Last one".to_owned(), "End.".to_owned()),
]
);
assert_eq!(super::outline("# Only a title\n"), Outline::default());
assert_eq!(super::outline("# T\n\n```\ncode\n```\n").summary, "");
}
#[test]
fn shorten_cuts_at_a_word_or_between_characters() {
assert_eq!(shorten("short", 10), "short");
assert_eq!(shorten("exactly ten", 11), "exactly ten");
assert_eq!(shorten("one two three four", 12), "one two…");
assert_eq!(shorten("one, two three four", 5), "one…");
let japanese = "あ".repeat(200);
let cut = shorten(&japanese, 160);
assert_eq!(cut.chars().count(), 160);
assert!(cut.ends_with('…'));
let long_word = "a".repeat(300);
assert_eq!(shorten(&long_word, 160).chars().count(), 160);
}
#[test]
fn json_ld_cannot_end_its_script_element() {
let data = serde_json::json!({
"name": "</script><script>alert(1)</script>",
"text": "a & b <!-- c --> \"quoted\" \u{2028}",
});
let element = TrustedHtml::json_ld(&data);
let text = element.as_str();
let inner = text
.strip_prefix("<script type=\"application/ld+json\">")
.and_then(|rest| rest.strip_suffix("</script>"))
.expect("one script element");
assert!(!inner.contains(['<', '>', '&']), "{inner}");
let parsed: serde_json::Value = serde_json::from_str(inner).expect("valid JSON");
assert_eq!(parsed, data);
}
}