Skip to content

Commit

Permalink
fix(templ): fix delimiter usage
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Oct 9, 2024
1 parent ba457cf commit ac5d606
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 15 deletions.
1 change: 1 addition & 0 deletions crates/rari-doc/src/pages/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ fn build_doc(doc: &Doc) -> Result<BuiltDocy, DocError> {
},
browser_compat: doc.meta.browser_compat.clone(),
other_translations,
page_type: doc.meta.page_type,
},
url: doc.meta.url.clone(),
..Default::default()
Expand Down
3 changes: 3 additions & 0 deletions crates/rari-doc/src/pages/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::path::PathBuf;

use chrono::{DateTime, NaiveDate, NaiveDateTime, Utc};
use rari_data::baseline::SupportStatusWithByKey;
use rari_types::fm_types::PageType;
use rari_types::locale::{Locale, Native};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -114,6 +115,8 @@ pub struct JsonDoc {
pub baseline: Option<&'static SupportStatusWithByKey>,
#[serde(rename = "browserCompat", skip_serializing_if = "Vec::is_empty")]
pub browser_compat: Vec<String>,
#[serde(rename = "pageType")]
pub page_type: PageType,
}

#[derive(Debug, Clone, Serialize)]
Expand Down
6 changes: 2 additions & 4 deletions crates/rari-doc/src/templ/render.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::Write;

use rari_md::ext::{DELIM_END, DELIM_START};
use rari_types::globals::deny_warnings;
use rari_types::{AnyArg, RariEnv};
use tracing::{span, warn, Level};
Expand All @@ -8,9 +9,6 @@ use super::parser::{parse, Token};
use super::templs::invoke;
use crate::error::DocError;

static DELIM_START: &str = ";!::::";
static DELIM_END: &str = ";!::::";

pub struct Rendered {
pub content: String,
pub templs: Vec<String>,
Expand Down Expand Up @@ -117,7 +115,7 @@ pub fn render_and_decode_ref(env: &RariEnv, input: &str) -> Result<String, DocEr

pub(crate) fn decode_ref(input: &str, templs: &[String]) -> Result<String, DocError> {
let mut decoded = String::with_capacity(input.len());
if !input.contains(";!::::") {
if !input.contains(DELIM_START) {
return Ok(input.to_string());
}
let mut frags = vec![];
Expand Down
3 changes: 3 additions & 0 deletions crates/rari-md/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ pub(crate) enum Flag {
Card,
None,
}

pub static DELIM_START: &str = ";!::::";
pub static DELIM_END: &str = ";!::::";
4 changes: 2 additions & 2 deletions crates/rari-md/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rari_types::locale::Locale;

use crate::anchor;
use crate::ctype::isspace;
use crate::ext::Flag;
use crate::ext::{Flag, DELIM_START};
use crate::node_card::{is_callout, NoteCard};

/// Formats an AST as HTML, modified by the given options.
Expand Down Expand Up @@ -598,7 +598,7 @@ impl<'o, 'c: 'o> HtmlFormatter<'o, 'c> {
let mut text_content = Vec::with_capacity(20);
Self::collect_first_child_text(node, &mut text_content);
let raw_id = String::from_utf8(text_content).unwrap();
let is_templ = raw_id.starts_with("!::::");
let is_templ = raw_id.starts_with(DELIM_START);
if is_templ {
write!(self.output, "<dt data-update-id")?;
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-md/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod anchor;
pub(crate) mod ctype;
pub(crate) mod dl;
pub mod error;
pub(crate) mod ext;
pub mod ext;
pub(crate) mod html;
pub mod node_card;
pub(crate) mod p;
Expand Down
19 changes: 11 additions & 8 deletions crates/rari-md/src/p.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
use comrak::nodes::{AstNode, NodeValue};
use itertools::Itertools;

use crate::ext::{DELIM_END, DELIM_START};

fn only_escaped_templ(b: &[u8], start: usize) -> bool {
let b = &b[..b
.iter()
.rev()
.find_position(|c| *c != &b'\n')
.map(|(i, _)| b.len() - i)
.unwrap_or(0)];
if b[start..].starts_with("!::::".as_bytes()) {
if b[start..].starts_with(DELIM_START.as_bytes()) {
let start = start + DELIM_START.as_bytes().len();
if let Some(end) = b[start..]
.windows(5)
.position(|window| window == "::::!".as_bytes())
.windows(DELIM_END.as_bytes().len())
.position(|window| window == DELIM_END.as_bytes())
{
if start + end + 5 == b.len() {
if start + end + DELIM_END.as_bytes().len() == b.len() {
return true;
} else {
return only_escaped_templ(b, start + end + 5);
return only_escaped_templ(b, start + end + DELIM_END.as_bytes().len());
}
}
}
Expand Down Expand Up @@ -51,11 +54,11 @@ mod test {

#[test]
fn test_only_escaped_templ() {
let b = b"!::::0::::!";
let b = b";!::::0;!::::";
assert!(only_escaped_templ(b, 0));
let b = b"!::::0::::!!::::1::::!";
let b = b";!::::0;!::::;!::::1;!::::";
assert!(only_escaped_templ(b, 0));
let b = b"foo !::::0::::!!::::1::::!";
let b = b"foo ;!::::0;!::::;!::::1::::!;";
assert!(!only_escaped_templ(b, 0));
}
}

0 comments on commit ac5d606

Please sign in to comment.