Code highlighting test
Python
from dataclasses import dataclass
from typing import Optional
@dataclass
class Zettel:
"""A single note in the knowledge graph."""
id: str
title: str
tags: list[str]
body: Optional[str] = None
def has_tag(self, tag: str) -> bool:
return tag in self.tags
def __repr__(self) -> str:
return f"Zettel({self.id}: {self.title})"
def find_orphans(zettels: dict[str, Zettel], links: set[str]) -> list[Zettel]:
return [z for z in zettels.values() if z.id not in links]
TypeScript
interface Zettel {
id: string;
title: string;
tags: string[];
body?: string;
}
async function fetchZettels(baseUrl: string): Promise<Zettel[]> {
const response = await fetch(`${baseUrl}/api/zettels`);
if (!response.ok) {
throw new Error(`Failed to fetch: ${response.status}`);
}
const data: Zettel[] = await response.json();
return data.filter((z) => z.tags.length > 0);
}
const formatTag = (tag: string): string => `#${tag.toLowerCase()}`;
Rust
use std::collections::{HashMap, HashSet};
#[derive(Debug, Clone)]
struct Zettel {
id: String,
title: String,
tags: Vec<String>,
body: Option<String>,
}
impl Zettel {
fn new(id: &str, title: &str, tags: Vec<&str>) -> Self {
Self {
id: id.to_string(),
title: title.to_string(),
tags: tags.into_iter().map(String::from).collect(),
body: None,
}
}
fn has_tag(&self, tag: &str) -> bool {
self.tags.iter().any(|t| t == tag)
}
}
fn find_orphans<'a>(
zettels: &'a HashMap<String, Zettel>,
links: &HashSet<String>,
) -> Vec<&'a Zettel> {
zettels.values().filter(|z| !links.contains(&z.id)).collect()
}