2026 年 05 月 02 日

Today I made some filters that may or may not come into use sometime. I find the .eleventy.js to get cluttered very easily so I am practicing splitting it up into multiple files such as _11ty/. The filters seem to be the easiest for me to understand and make, shortcodes and collections are still a bit out of reach. Though, I'll be reading the docs and learning as I go.

Casing

toUpperCase(str) { return str.toUpperCase() },
toLowerCase(str) { return str.toLowerCase() },

These do exactly what you'd expect them to, and I haven't even used them yet...

Deslugify

export default {
  deslugify(str) {
    return str
      .replace(/[-_]+/g, " ") // replace fake spaces
      .replace(/\b\w/g, c => c.toUpperCase()) // upper case on new words
  }
}

Date

I use a helper function that is not exported to the .eleventy.js.

function safeDate(input) {
  const d = new Date(input)
  return isNaN(d) ? null : d
}
export default {
  toISOstring(inp) {
    const date = safeDate(inp) // validate
    return date ? date.toISOString() : ""
  }
}

Description

export default {
  genDescription(str) {
    if (!str) return "";

    let clean = str.replace(/<[^>]*>/g, "") // html
    clean = clean.replace(/^#{1,6}\s*/gm, "") // headings
    clean = clean.replace(/^>\s*/gm, "") // block quotes
    clean = clean.replace(/^\s*[-*+]\s+/gm, "") // lists
    clean = clean.replace(/\s+/g, " ").trim() // whitespace

    if (clean.length <= 120) return clean
    const short = clean.slice(0, 120)
    const trimmed = short.trimEnd().replace(/\s+\S*$/, "")

    return trimmed + "…";
  }
}