Helpers



Display the current year

<!-- 2021 -->

{{ hlp.11ty.year() }}


Strip HTML for content



{{ hlp.11ty.stripHtml(item.templateContent) | safe }}


Limit number of words

<!-- 2021 -->

{{ hlp.11ty.year() }}


Time ago, Human readable dates

<!-- Posted: 2 months ago -->

 Posted: {{ hlp.11ty.timeAgo(page.date) }}


11ty.js (src)

//

// @ts-check
'use strict'

const take = require('lodash.take')

const date = new Date()

const dayjs = require('dayjs')
const relativeTime = require('dayjs/plugin/relativeTime')

const currentYear = () => date.getFullYear()

const formatDate = (date) => new Date(date).toDateString()

const stripHtml = (data = null) => {
  if (data === null || data === undefined) return
  return data.replace(/(<([^>]+)>)/ig, '')
}

const limit = ($arr = [], $limit = 3) => {
  if ($arr.length >= 1) { return take($arr, $limit) }
  return null
}

const timeAgo = (date) => {
  dayjs.extend(relativeTime)
  return dayjs(date).fromNow()
}

module.exports = {
  year: currentYear,
  currentYear,
  formatDate,
  strip: stripHtml,
  stripHtml, // deprecated use strip
  limit,
  timeAgo

}