mirror of
https://github.com/thegeneralist01/archivr
synced 2026-12-04 18:56:44 +01:00
fix: fence plain text in Markdown exports
This commit is contained in:
parent
cfc87e9859
commit
8c8c084e9c
3 changed files with 60 additions and 7 deletions
|
|
@ -55,6 +55,22 @@ fn poster_ref(poster: &std::path::Path) -> String {
|
|||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn fenced_plain_text(text: &str) -> String {
|
||||
let mut longest_run = 0;
|
||||
let mut run = 0;
|
||||
for byte in text.bytes() {
|
||||
if byte == b'`' {
|
||||
run += 1;
|
||||
longest_run = longest_run.max(run);
|
||||
} else {
|
||||
run = 0;
|
||||
}
|
||||
}
|
||||
let fence = "`".repeat((longest_run + 1).max(3));
|
||||
let trailing_newline = if text.ends_with('\n') { "" } else { "\n" };
|
||||
format!("{fence}\n{text}{trailing_newline}{fence}\n")
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Public API
|
||||
// ---------------------------------------------------------------------------
|
||||
|
|
@ -155,9 +171,12 @@ fn render_body_md(
|
|||
}
|
||||
ExportBody::Pdf(asset) => format!("[PDF document]({})\n", asset_ref(asset)),
|
||||
ExportBody::Page(asset) => format!("[Archived page snapshot]({})\n", asset_ref(asset)),
|
||||
ExportBody::Text { text, .. } => {
|
||||
// Verbatim text.
|
||||
format!("{}\n", text)
|
||||
ExportBody::Text { mime_markdown, text } => {
|
||||
if *mime_markdown {
|
||||
format!("{text}\n")
|
||||
} else {
|
||||
fenced_plain_text(text)
|
||||
}
|
||||
}
|
||||
ExportBody::TextTooLarge(asset) | ExportBody::File(asset) => {
|
||||
let ext = ext_of_s(&asset.name).to_uppercase();
|
||||
|
|
|
|||
|
|
@ -718,6 +718,15 @@ mod tests {
|
|||
|
||||
/// Create a text entry with a .txt primary_media artifact. Returns entry_uid.
|
||||
fn add_text_entry(&self, title: &str, content: &str) -> String {
|
||||
self.add_text_entry_with_extension(title, content, "txt")
|
||||
}
|
||||
|
||||
fn add_text_entry_with_extension(
|
||||
&self,
|
||||
title: &str,
|
||||
content: &str,
|
||||
extension: &str,
|
||||
) -> String {
|
||||
let conn = self.conn();
|
||||
let n = self.si_counter.get();
|
||||
self.si_counter.set(n + 1);
|
||||
|
|
@ -746,7 +755,7 @@ mod tests {
|
|||
let hash = format!("{n:064x}");
|
||||
let sub1 = &hash[0..1];
|
||||
let sub2 = &hash[1..2];
|
||||
let relpath = format!("raw/{sub1}/{sub2}/{hash}.txt");
|
||||
let relpath = format!("raw/{sub1}/{sub2}/{hash}.{extension}");
|
||||
let file_path = self.paths.store_path.join(&relpath);
|
||||
std::fs::create_dir_all(file_path.parent().unwrap()).unwrap();
|
||||
std::fs::write(&file_path, content).unwrap();
|
||||
|
|
@ -904,6 +913,31 @@ mod tests {
|
|||
assert!(md.contains("# heading\n\nbody text"), "body verbatim");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn markdown_export_fences_plain_text_but_keeps_markdown_source_raw() {
|
||||
let ctx = TestCtx::new();
|
||||
let source = "# heading\n[link](https://example.com)\n<script>alert(1)</script>\n```\ninner\n```";
|
||||
let plain_uid = ctx.add_text_entry("Plain text", source);
|
||||
let markdown_uid = ctx.add_text_entry_with_extension("Markdown", source, "md");
|
||||
let opts = ExportOptions {
|
||||
format: ExportFormat::Markdown,
|
||||
layout: ExportLayout::Together,
|
||||
include_credit: false,
|
||||
caller_bits: u32::MAX,
|
||||
};
|
||||
|
||||
let plain = export_entries(&ctx.paths, &[plain_uid], &opts).unwrap();
|
||||
let (file, _, _, _) = plain.open().unwrap();
|
||||
let plain_md = read_zip_entry(file, "index.md");
|
||||
assert!(plain_md.contains(&format!("````\n{source}\n````")));
|
||||
|
||||
let markdown = export_entries(&ctx.paths, &[markdown_uid], &opts).unwrap();
|
||||
let (file, _, _, _) = markdown.open().unwrap();
|
||||
let markdown_md = read_zip_entry(file, "index.md");
|
||||
assert!(markdown_md.contains(&format!("\n\n{source}\n")));
|
||||
assert!(!markdown_md.contains("````\n"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parent_child_dedupe() {
|
||||
let ctx = TestCtx::new();
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ pub(crate) enum ExportBody {
|
|||
Pdf(Asset),
|
||||
/// An HTML/HTM snapshot (SingleFile page).
|
||||
Page(Asset),
|
||||
Text { _mime_markdown: bool, text: String },
|
||||
Text { mime_markdown: bool, text: String },
|
||||
/// Text file > 5 MiB: offer a download link only.
|
||||
TextTooLarge(Asset),
|
||||
File(Asset),
|
||||
|
|
@ -287,8 +287,8 @@ fn body_from_primary_media(
|
|||
match std::fs::read(&asset.source) {
|
||||
Ok(bytes) => {
|
||||
let text = String::from_utf8_lossy(&bytes).into_owned();
|
||||
let _mime_markdown = ext == "md" || ext == "markdown";
|
||||
ExportBody::Text { _mime_markdown, text }
|
||||
let mime_markdown = ext == "md" || ext == "markdown";
|
||||
ExportBody::Text { mime_markdown, text }
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("warn: export: failed to read text artifact: {e:#}");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue