Skip to content

Update comrak to 0.2.3, use ext_header_ids #1129

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

Merged
merged 7 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ serde_derive = "1.0.0"
serde = "1.0.0"
clippy = { version = "=0.0.162", optional = true }
chrono = { version = "0.4.0", features = ["serde"] }
comrak = { version = "0.1.9", default-features = false }
ammonia = "0.7.0"
comrak = { version = "0.2.3", default-features = false }
ammonia = { git = "https://github.com/notriddle/ammonia" }
docopt = "0.8.1"
itertools = "0.6.0"
lettre = "0.6"
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/crate/version.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Controller from '@ember/controller';
import PromiseProxyMixin from '@ember/object/promise-proxy-mixin';
import ArrayProxy from '@ember/array/proxy';
import { computed } from '@ember/object';
import { computed, observer } from '@ember/object';
import { later } from '@ember/runloop';
import $ from 'jquery';
import moment from 'moment';

const NUM_VERSIONS = 5;
Expand Down Expand Up @@ -173,4 +174,8 @@ export default Controller.extend({
},
},

report: observer('crate.readme', function() {
setTimeout(() => $(window).trigger('hashchange'));
}),

});
50 changes: 50 additions & 0 deletions app/initializers/hashchange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import $ from 'jquery';

function decodeFragmentValue(hash) {
try {
return decodeURIComponent(hash.slice(1));
} catch(_) {
return '';
}
}

function findElementByFragmentName(document, name) {
if (name === '') {
return;
}

return document.getElementById(name) || document.getElementsByName(name)[0];
}

function hashchange() {
if (document.querySelector(':target')) {
return;
}

const hash = decodeFragmentValue(location.hash);
const target = findElementByFragmentName(document, `user-content-${hash}`);
if (target) {
target.scrollIntoView();
}
}

export function initialize() {
$(window).on('hashchange', hashchange);

// If clicking on a link to the same fragment as currently in the address bar,
// hashchange won't be fired, so we need to manually trigger rescroll.
$(document).on('a[href]', 'click', function(event) {
if (this.href === location.href && location.hash.length > 1) {
setTimeout(function() {
if (!event.defaultPrevented) {
hashchange();
}
});
}
});
}

export default {
name: 'app.hashchange',
initialize
};
45 changes: 32 additions & 13 deletions src/render.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use ammonia::Ammonia;
use ammonia::Builder;
use comrak;

use util::CargoResult;

/// Context for markdown to HTML rendering.
#[allow(missing_debug_implementations)]
pub struct MarkdownRenderer<'a> {
html_sanitizer: Ammonia<'a>,
html_sanitizer: Builder<'a>,
}

impl<'a> MarkdownRenderer<'a> {
Expand Down Expand Up @@ -53,8 +53,7 @@ impl<'a> MarkdownRenderer<'a> {
.cloned()
.collect();
let tag_attributes = [
("a", ["href", "target"].iter().cloned().collect()),
("code", ["class"].iter().cloned().collect()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, does allowed_classes make this line unnecessary...? I guess so since the tests pass!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, it even became required in the newer ammonia: https://github.com/notriddle/ammonia/blob/2581eec04ef98419fd202e0dd4d8997c9dfdb06e/src/lib.rs#L1030-L1037 (you're allowed to use allowed_classes or set class as an allowed tag, but not both)

("a", ["href", "id", "target"].iter().cloned().collect()),
(
"img",
["width", "height", "src", "alt", "align"]
Expand Down Expand Up @@ -94,14 +93,13 @@ impl<'a> MarkdownRenderer<'a> {
].iter()
.cloned()
.collect();
let html_sanitizer = Ammonia {
link_rel: Some("nofollow noopener noreferrer"),
keep_cleaned_elements: true,
tags: tags,
tag_attributes: tag_attributes,
allowed_classes: allowed_classes,
..Ammonia::default()
};
let mut html_sanitizer = Builder::new();
html_sanitizer
.link_rel(Some("nofollow noopener noreferrer"))
.tags(tags)
.tag_attributes(tag_attributes)
.allowed_classes(allowed_classes)
.id_prefix(Some("user-content-"));
MarkdownRenderer {
html_sanitizer: html_sanitizer,
}
Expand All @@ -115,10 +113,11 @@ impl<'a> MarkdownRenderer<'a> {
ext_table: true,
ext_tagfilter: true,
ext_tasklist: true,
ext_header_ids: Some("user-content-".to_string()),
..comrak::ComrakOptions::default()
};
let rendered = comrak::markdown_to_html(text, &options);
Ok(self.html_sanitizer.clean(&rendered))
Ok(self.html_sanitizer.clean(&rendered).to_string())
}
}

Expand Down Expand Up @@ -218,4 +217,24 @@ mod tests {
let result = markdown_to_html(text).unwrap();
assert_eq!(result, "<p>Hello World!</p>\n");
}

#[test]
fn header_has_tags() {
let text = "# My crate\n\nHello, world!\n";
let result = markdown_to_html(text).unwrap();
assert_eq!(
result,
"<h1><a href=\"#my-crate\" id=\"user-content-my-crate\" rel=\"nofollow noopener noreferrer\"></a>My crate</h1>\n<p>Hello, world!</p>\n"
);
}

#[test]
fn manual_anchor_is_sanitized() {
let text = "<h1><a href=\"#my-crate\" id=\"my-crate\"></a>My crate</h1>\n<p>Hello, world!</p>\n";
let result = markdown_to_html(text).unwrap();
assert_eq!(
result,
"<h1><a href=\"#my-crate\" id=\"user-content-my-crate\" rel=\"nofollow noopener noreferrer\"></a>My crate</h1>\n<p>Hello, world!</p>\n"
);
}
}