Code Blocks
January 2021 (427 Words, 3 Minutes)
A showcase of syntax highlighting across languages.
Python
from pathlib import Path
import json
def load_config(path: str) -> dict:
"""Read a JSON config file and return its contents."""
config_path = Path(path)
if not config_path.exists():
raise FileNotFoundError(f"Config not found: {path}")
return json.loads(config_path.read_text())
config = load_config("settings.json")
print(config.get("name", "world"))
Ruby
require "net/http"
require "json"
module Weather
BASE_URL = "https://api.weather.example.com"
def self.fetch(city)
uri = URI("#{BASE_URL}/current?city=#{city}")
response = Net::HTTP.get_response(uri)
unless response.is_a?(Net::HTTPSuccess)
raise "Request failed: #{response.code}"
end
JSON.parse(response.body, symbolize_names: true)
end
end
data = Weather.fetch("Tokyo")
puts "#{data[:city]}: #{data[:temp_c]}C"
JavaScript
async function fetchPosts(tag) {
const url = new URL("https://api.example.com/posts");
url.searchParams.set("tag", tag);
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch posts: ${response.status}`);
}
const { posts } = await response.json();
return posts.map(({ title, slug }) => ({ title, slug }));
}
const posts = await fetchPosts("javascript");
console.log(`Found ${posts.length} posts`);
Elixir
defmodule TaskRunner do
@moduledoc "Runs async tasks with a timeout."
def run(tasks, timeout \\ 5_000) do
tasks
|> Enum.map(&Task.async/1)
|> Enum.map(&Task.await(&1, timeout))
end
end
results =
TaskRunner.run([
fn -> HTTPClient.get("/users") end,
fn -> HTTPClient.get("/posts") end
])
IO.inspect(results, label: "results")
CSS
.highlight, pre code, blockquote {
border-radius: 0.5em;
}
blockquote {
background-color: var(--bg-secondary);
border: 1px var(--border) solid;
}