// Scanning: which files are pages, what they are called and where they go.
///|
/// Directories that must never be scanned as content, relative to the content
/// root.
///
/// A configured directory that lives outside that root (the output folder of a
/// site whose `srcDir` is a subdirectory, for instance) is dropped. It cannot
/// match a scanned path anyway, and keeping its name would hide a content
/// folder that happens to share it — `content/public` with `dest: public`.
///
/// `static` is here because a site rooted at the site root keeps its asset
/// folder next to its pages. Without it every static file was copied twice:
/// once as content (`/static/app.css`) and once as an asset (`/app.css`).
pub fn content_prefixes(cfg : @config.Config) -> Array[String] {
let source = @core.collapse_dots(cfg.source)
let result : Array[String] = []
for
dir in [
cfg.layouts_dir,
cfg.includes_dir,
cfg.data_dir,
cfg.static_dir,
cfg.dest,
] {
let path = @core.collapse_dots(dir)
if path == "" || path == source {
continue
}
if source == "" {
result.push(path)
} else if path.has_prefix("\{source}/") {
result.push(@core.slice(path, source.length() + 1, path.length()))
}
}
result
}
///|
/// Check whether a path has a configured page extension.
pub fn is_page_file(path : StringView, cfg : @config.Config) -> Bool {
let ext = @core.extname(path)
let bare = if ext.has_prefix(".") {
@core.slice(ext, 1, ext.length())
} else {
ext
}
cfg.page_exts.contains(bare)
}
///|
/// Check whether a path is ignored or inside an excluded prefix.
pub fn is_excluded(
path : StringView,
cfg : @config.Config,
prefixes : Array[String],
) -> Bool {
let full = @core.normalize_slashes(path)
let root = @core.normalize_slashes(cfg.source_path())
let relative = if full.has_prefix(root) {
let tail = @core.slice(full, root.length(), full.length())
if tail.has_prefix("/") {
@core.slice(tail, 1, tail.length())
} else {
tail
}
} else {
full
}
if cfg.is_ignored(relative) {
return true
}
for prefix in prefixes {
if prefix == "" ||
relative == prefix ||
relative.has_prefix("\{prefix}/") ||
full.has_prefix("\{root}/\{prefix}/") {
return true
}
}
false
}
///|
/// Extract an ISO date from a file name (`2026-12-01-post.md`).
pub fn date_from_name(name : StringView) -> String {
if name.length() >= 10 {
let d = @core.slice(name, 0, 10)
if @core.to_int(@core.slice(d, 0, 4)) > 1900 && d[4] == '-' && d[7] == '-' {
return d
}
}
""
}
///|
/// Split a source path into its language and the path inside that language.
///
/// `zh/about.md` belongs to `zh` when `zh` is configured; every other path
/// belongs to the default language and is returned unchanged. A site without an
/// `i18n` block returns `("", path)`, so nothing changes for single-language
/// sites.
pub fn split_language(
path : StringView,
cfg : @config.Config,
) -> (String, String) {
if !cfg.is_multilingual() {
return ("", path.to_owned())
}
match @core.split_once(@core.normalize_slashes(path), '/') {
Some((head, tail)) =>
if cfg.has_lang(head) {
(head, tail)
} else {
(cfg.default_lang, path.to_owned())
}
None => (cfg.default_lang, path.to_owned())
}
}
///|
/// Expand permalink placeholders into a final URL.
///
/// Supported placeholders: `:path`, `:slug`, `:collection`, `:title`, `:date`,
/// `:year`, `:month`, `:day`. A template of `false` or `none` skips the page.
pub fn resolve_permalink(
cfg : @config.Config,
front : Json,
no_ext : String,
) -> String {
let explicit = str_field(front, "permalink", "")
let template = if explicit != "" { explicit } else { cfg.permalink }
if template == "false" || template == "none" {
return ""
}
let slug = str_field(front, "slug", @core.basename(no_ext))
let collection = str_field(front, "collection", @core.dirname(no_ext))
let date = str_field(front, "date", date_from_name(no_ext))
let title = str_field(front, "title", slug)
let clean_path = if no_ext == "index" {
""
} else if no_ext.has_suffix("/index") {
@core.slice(no_ext, 0, no_ext.length() - 6)
} else {
no_ext
}
let result = template
.replace_all(old=":path", new=clean_path)
.replace_all(old=":slug", new=slug)
.replace_all(old=":collection", new=collection)
.replace_all(old=":title", new=@core.slugify(title))
.replace_all(old=":date", new=date)
.replace_all(
old=":year",
new=if date.length() >= 4 { @core.slice(date, 0, 4) } else { "" },
)
.replace_all(
old=":month",
new=if date.length() >= 7 { @core.slice(date, 5, 7) } else { "" },
)
.replace_all(
old=":day",
new=if date.length() >= 10 { @core.slice(date, 8, 10) } else { "" },
)
// The root page has an empty `:path`, so `/:path/` would produce `//`.
let result = if result.has_prefix("//") {
@core.slice(result, 1, result.length())
} else {
result
}
if result.has_suffix(".html") ||
result.has_suffix(".xml") ||
result.has_suffix(".json") ||
result.has_suffix(".txt") {
result
} else if result.has_suffix("/") {
result
} else {
"\{result}/"
}
}
///|
/// The public URL of a source file, language prefix included.
///
/// `check` resolves URLs through this too, so duplicate detection and the
/// messages it prints agree with what a build would actually produce. Resolving
/// from the raw path instead made default-language pages look like they lived
/// under `/en/`.
pub fn page_url(cfg : @config.Config, front : Json, rel : StringView) -> String {
let (lang, page_rel) = split_language(rel, cfg)
let url = resolve_permalink(cfg, front, @core.strip_ext(page_rel))
if url == "" || lang == "" || lang == cfg.default_lang {
url
} else {
"\{cfg.lang_prefix(lang)}\{url}"
}
}
///|
/// Translate a site URL into an output-relative file path.
pub fn url_to_dest(url : StringView) -> String {
let path = if url.has_prefix("/") {
@core.slice(url, 1, url.length())
} else {
url.to_owned()
}
if path == "" || path.has_suffix("/") {
"\{path}index.html"
} else {
path
}
}