pub fn language_token(lang: &str) -> &str {
lang
.split(|c: char| c.is_whitespace() || c == ',')
.next()
.unwrap_or("")
}
fn is_example_lang(lang: &str) -> bool {
matches!(
lang,
"js"
| "javascript"
| "mjs"
| "cjs"
| "jsx"
| "ts"
| "typescript"
| "mts"
| "cts"
| "tsx"
)
}
pub struct ExampleCode {
pub displayed: String,
pub copyable: String,
}
pub fn process_example_code(
lang: Option<&str>,
code: &str,
) -> Option<ExampleCode> {
let lang = language_token(lang.unwrap_or(""));
if !is_example_lang(lang) {
return None;
}
let mut displayed = String::new();
let mut copyable = String::new();
for line in code.split_inclusive('\n') {
let (content, newline) = match line.strip_suffix('\n') {
Some(content) => (content, "\n"),
None => (line, ""),
};
let trimmed = content.trim_start();
let indent = &content[..content.len() - trimmed.len()];
if let Some(rest) = trimmed.strip_prefix("##") {
displayed.push_str(indent);
displayed.push('#');
displayed.push_str(rest);
displayed.push_str(newline);
copyable.push_str(indent);
copyable.push('#');
copyable.push_str(rest);
copyable.push_str(newline);
} else if trimmed == "#" {
copyable.push_str(indent);
copyable.push_str(newline);
} else if let Some(rest) = trimmed.strip_prefix("# ") {
copyable.push_str(indent);
copyable.push_str(rest);
copyable.push_str(newline);
} else {
displayed.push_str(line);
copyable.push_str(line);
}
}
Some(ExampleCode {
displayed,
copyable,
})
}
#[cfg(test)]
mod tests {
use super::*;
fn displayed(lang: Option<&str>, code: &str) -> Option<String> {
process_example_code(lang, code).map(|c| c.displayed)
}
fn copyable(lang: Option<&str>, code: &str) -> Option<String> {
process_example_code(lang, code).map(|c| c.copyable)
}
#[test]
fn hides_lines() {
let code = "# import { add } from \"./mod.ts\";\nadd(1, 2);\n";
assert_eq!(displayed(Some("ts"), code).unwrap(), "add(1, 2);\n");
assert_eq!(
copyable(Some("ts"), code).unwrap(),
"import { add } from \"./mod.ts\";\nadd(1, 2);\n"
);
}
#[test]
fn hides_bare_hash_line() {
let code = "const x = 1;\n#\nconst y = 2;\n";
assert_eq!(
displayed(Some("js"), code).unwrap(),
"const x = 1;\nconst y = 2;\n"
);
assert_eq!(
copyable(Some("js"), code).unwrap(),
"const x = 1;\n\nconst y = 2;\n"
);
}
#[test]
fn escapes_double_hash() {
let code = "## not hidden\n";
assert_eq!(displayed(Some("ts"), code).unwrap(), "# not hidden\n");
assert_eq!(copyable(Some("ts"), code).unwrap(), "# not hidden\n");
}
#[test]
fn preserves_indentation() {
let code = "function f() {\n # const secret = 1;\n return 2;\n}\n";
assert_eq!(
displayed(Some("ts"), code).unwrap(),
"function f() {\n return 2;\n}\n"
);
assert_eq!(
copyable(Some("ts"), code).unwrap(),
"function f() {\n const secret = 1;\n return 2;\n}\n"
);
}
#[test]
fn leaves_private_fields_and_attributes_alone() {
let code = "class C {\n #x = 1;\n}\n";
assert_eq!(displayed(Some("ts"), code).unwrap(), code);
assert_eq!(copyable(Some("ts"), code).unwrap(), code);
}
#[test]
fn preserves_shebang() {
let code = "#!/usr/bin/env -S deno run\nconsole.log(1);\n";
assert_eq!(displayed(Some("ts"), code).unwrap(), code);
}
#[test]
fn ignores_non_example_languages() {
let code = "# install\ndeno install\n";
assert!(process_example_code(Some("sh"), code).is_none());
assert!(process_example_code(Some("bash"), code).is_none());
assert!(process_example_code(None, code).is_none());
}
#[test]
fn handles_attributes_in_info_string() {
let code = "# hidden;\nshown;\n";
assert_eq!(displayed(Some("ts ignore"), code).unwrap(), "shown;\n");
}
#[test]
fn language_token_strips_directive_separators() {
assert_eq!(language_token("ts"), "ts");
assert_eq!(language_token("ts no-eval"), "ts");
assert_eq!(language_token("ts,"), "ts");
assert_eq!(language_token("ts, no-eval"), "ts");
assert_eq!(language_token("ts,no-eval"), "ts");
assert_eq!(language_token(""), "");
}
#[test]
fn handles_comma_separated_info_string() {
let code = "# hidden;\nshown;\n";
assert_eq!(displayed(Some("ts,"), code).unwrap(), "shown;\n");
assert_eq!(displayed(Some("ts, no-eval"), code).unwrap(), "shown;\n");
}
#[test]
fn handles_missing_trailing_newline() {
let code = "# hidden;\nshown;";
assert_eq!(displayed(Some("ts"), code).unwrap(), "shown;");
assert_eq!(copyable(Some("ts"), code).unwrap(), "hidden;\nshown;");
}
}