-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(markdown): add support for math rendering with typst and katex #2791
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
base: next
Are you sure you want to change the base?
Changes from 16 commits
6f3df32
f4373cc
c62de4c
47c4852
e64b7b2
a2760b9
6b6448b
4444203
1b595d2
0a33bda
242f974
8f2bc47
052b432
733ea26
a97d342
df62c23
3c84dd8
3cd9bd0
cf3af3a
54a444e
51ba6d9
72a3517
fd349a4
1ec97e3
3ed02a5
40c79a2
7ba158a
ec4bc5c
ee72c04
b93c762
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use bincode; | ||
use dashmap::DashMap; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fs::{self, File, OpenOptions}; | ||
use std::hash::Hash; | ||
|
||
use std::io::{self, Read, Write}; | ||
use std::path::{Path, PathBuf}; | ||
|
||
/// Generic cache using DashMap, storing data in a binary file | ||
#[derive(Debug)] | ||
pub struct GenericCache<K, V> | ||
where | ||
K: Eq + Hash + Serialize + for<'de> Deserialize<'de>, | ||
V: Serialize + for<'de> Deserialize<'de>, | ||
{ | ||
cache_file: PathBuf, | ||
cache: DashMap<K, V>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm not totally sure whether it matters at the kind of scale/concurrency zola operates on, but you might find papaya a better fit for a concurrent hash map. this (biased) blogpost by the author totally convinced me. |
||
} | ||
|
||
impl<K, V> GenericCache<K, V> | ||
where | ||
K: Eq + Hash + Serialize + for<'de> Deserialize<'de>, | ||
V: Serialize + for<'de> Deserialize<'de>, | ||
{ | ||
/// Get the directory where the cache is stored | ||
pub fn dir(&self) -> &Path { | ||
self.cache_file.parent().unwrap() | ||
} | ||
|
||
/// Create a new cache for a specific type | ||
pub fn new(base_cache_dir: &Path, filename: &str) -> crate::Result<Self> { | ||
// Create the base cache directory | ||
fs::create_dir_all(base_cache_dir)?; | ||
cestef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Full path to the cache file | ||
let cache_file = base_cache_dir.join(filename); | ||
|
||
// Attempt to load existing cache | ||
let cache = match Self::read_cache(&cache_file) { | ||
Ok(loaded_cache) => { | ||
println!("Loaded cache from {:?} ({:?})", cache_file, loaded_cache.len()); | ||
loaded_cache | ||
} | ||
Err(e) => { | ||
println!("Failed to load cache: {}", e); | ||
DashMap::new() | ||
} | ||
}; | ||
|
||
Ok(Self { cache_file, cache }) | ||
} | ||
|
||
/// Read cache from file | ||
fn read_cache(cache_file: &Path) -> io::Result<DashMap<K, V>> { | ||
if !cache_file.exists() { | ||
return Ok(DashMap::new()); | ||
} | ||
|
||
let mut file = File::open(cache_file)?; | ||
let mut buffer = Vec::new(); | ||
file.read_to_end(&mut buffer)?; | ||
|
||
bincode::deserialize(&buffer).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) | ||
} | ||
|
||
/// Write cache to file | ||
pub fn write(&self) -> io::Result<()> { | ||
let serialized = bincode::serialize(&self.cache) | ||
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; | ||
|
||
let mut file = | ||
OpenOptions::new().write(true).create(true).truncate(true).open(&self.cache_file)?; | ||
|
||
file.write_all(&serialized)?; | ||
Ok(()) | ||
} | ||
|
||
/// Get a reference to the underlying DashMap | ||
pub fn inner(&self) -> &DashMap<K, V> { | ||
&self.cache | ||
} | ||
|
||
pub fn get(&self, key: &K) -> Option<V> | ||
where | ||
V: Clone, | ||
{ | ||
self.cache.get(key).map(|r| r.value().clone()) | ||
} | ||
|
||
pub fn insert(&self, key: K, value: V) { | ||
self.cache.insert(key, value); | ||
} | ||
|
||
/// Clear the cache and remove the file | ||
pub fn clear(&self) -> crate::Result<()> { | ||
self.cache.clear(); | ||
|
||
if self.cache_file.exists() { | ||
fs::remove_file(&self.cache_file)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.