Skip to content

Commit

Permalink
Merge pull request #360 from philomena-dev/comrak-0.29
Browse files Browse the repository at this point in the history
Update to Philomena comrak v0.29.0
  • Loading branch information
liamwhite authored Nov 9, 2024
2 parents ba66c93 + a21189a commit 60f51c6
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 49 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ jobs:
- name: cargo clippy
run: (cd native/philomena && cargo clippy -- -D warnings)

- name: cargo test
run: (cd native/philomena && cargo test)

lint-and-test:
name: 'JavaScript Linting and Unit Tests'
runs-on: ubuntu-latest
Expand Down
90 changes: 49 additions & 41 deletions native/philomena/Cargo.lock

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

2 changes: 1 addition & 1 deletion native/philomena/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ crate-type = ["dylib"]

[dependencies]
base64 = "0.21"
comrak = { git = "https://github.com/philomena-dev/comrak", branch = "main", default-features = false }
comrak = { git = "https://github.com/philomena-dev/comrak", branch = "philomena-0.29.0", default-features = false }
http = "0.2"
jemallocator = { version = "0.5.0", features = ["disable_initial_exec_tls"] }
regex = "1"
Expand Down
20 changes: 20 additions & 0 deletions native/philomena/src/domains.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use http::Uri;
use regex::Regex;
use std::env;

pub fn get() -> Option<Vec<String>> {
Expand All @@ -12,3 +14,21 @@ pub fn get() -> Option<Vec<String>> {

None
}

pub fn relativize(domains: &[String], url: &str) -> Option<String> {
let uri = url.parse::<Uri>().ok()?;

if let Some(a) = uri.authority() {
if domains.contains(&a.host().to_string()) {
if let Ok(re) = Regex::new(&format!(r#"^http(s)?://({})"#, regex::escape(a.host()))) {
return Some(re.replace(url, "").into());
}
}
}

Some(url.into())
}

pub fn relativize_careful(domains: &[String], url: &str) -> String {
relativize(domains, url).unwrap_or_else(|| url.into())
}
2 changes: 2 additions & 0 deletions native/philomena/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::collections::HashMap;
mod camo;
mod domains;
mod markdown;
#[cfg(test)]
mod tests;
mod zip;

#[global_allocator]
Expand Down
31 changes: 24 additions & 7 deletions native/philomena/src/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,54 @@
use crate::{camo, domains};
use comrak::Options;
use std::collections::HashMap;
use std::sync::Arc;

fn common_options() -> Options {
pub fn common_options() -> Options {
let mut options = Options::default();

// Upstream options
options.extension.autolink = true;
options.extension.table = true;
options.extension.description_lists = true;
options.extension.superscript = true;
options.extension.strikethrough = true;
options.extension.philomena = true;
options.parse.smart = true;
options.render.hardbreaks = true;
options.render.github_pre_lang = true;
options.render.escape = true;

options.extension.camoifier = Some(|s| camo::image_url_careful(&s));
options.extension.philomena_domains = domains::get();
// Philomena options
options.extension.underline = true;
options.extension.spoiler = true;
options.extension.greentext = true;
options.extension.subscript = true;
options.extension.philomena = true;
options.render.ignore_empty_links = true;
options.render.ignore_setext = true;

options.extension.image_url_rewriter = Some(Arc::new(|url: &str| camo::image_url_careful(url)));

if let Some(domains) = domains::get() {
options.extension.link_url_rewriter = Some(Arc::new(move |url: &str| {
domains::relativize_careful(&domains, url)
}));
}

options
}

pub fn to_html(input: &str, reps: HashMap<String, String>) -> String {
let mut options = common_options();
options.render.escape = true;
options.extension.philomena_replacements = Some(reps);
options.extension.replacements = Some(reps);

comrak::markdown_to_html(input, &options)
}

pub fn to_html_unsafe(input: &str, reps: HashMap<String, String>) -> String {
let mut options = common_options();
options.render.escape = false;
options.render.unsafe_ = true;
options.extension.philomena_replacements = Some(reps);
options.extension.replacements = Some(reps);

comrak::markdown_to_html(input, &options)
}
Loading

0 comments on commit 60f51c6

Please sign in to comment.