Skip to content
Documentation menu

Writing Content

Code samples

Sample page: the same small function in Swift, C#, Python and Bash, to check syntax highlighting.

Justin Topham 2 min read

Sample content. This page exists to show how code looks in the theme. It has a date, so it also appears on the Blog page, and it is marked featured: true, so it is listed in /featured.json.

Each block below does the same thing: it takes a list of page titles and returns the ones that contain a search word.

Swift

struct Page {
    let title: String
    let weight: Int
}

/// Returns the pages whose title contains the search word, lightest first.
func search(_ pages: [Page], for word: String) -> [Page] {
    pages
        .filter { $0.title.localizedCaseInsensitiveContains(word) }
        .sorted { $0.weight < $1.weight }
}

let pages = [Page(title: "Install the theme", weight: 10), Page(title: "Menus", weight: 20)]
print(search(pages, for: "theme").map(\.title)) // ["Install the theme"]

C#

public record Page(string Title, int Weight);

public static class PageSearch
{
    // Returns the pages whose title contains the search word, lightest first.
    public static IEnumerable<Page> Search(IEnumerable<Page> pages, string word) =>
        pages
            .Where(p => p.Title.Contains(word, StringComparison.OrdinalIgnoreCase))
            .OrderBy(p => p.Weight);
}

var pages = new[] { new Page("Install the theme", 10), new Page("Menus", 20) };
Console.WriteLine(string.Join(", ", PageSearch.Search(pages, "theme").Select(p => p.Title)));

Python

from dataclasses import dataclass


@dataclass
class Page:
    title: str
    weight: int


def search(pages: list[Page], word: str) -> list[Page]:
    """Return the pages whose title contains the search word, lightest first."""
    matches = [p for p in pages if word.lower() in p.title.lower()]
    return sorted(matches, key=lambda p: p.weight)


pages = [Page("Install the theme", 10), Page("Menus", 20)]
print([p.title for p in search(pages, "theme")])  # ['Install the theme']

Bash

#!/usr/bin/env bash
# Prints the titles of the Markdown pages that mention the search word.
word="${1:?usage: search.sh WORD}"

grep --recursive --ignore-case --files-with-matches "$word" content/docs |
  while read -r file; do
    sed -n 's/^title: *"\(.*\)"/\1/p' "$file"
  done

How they compare

Language Filter Sort
Swift filter sorted
C# Where OrderBy
Python list comprehension sorted
Bash grep not sorted