From fcc2f4704e39b0e69b377cc138f75027721dac22 Mon Sep 17 00:00:00 2001 From: Dawid Rycerz Date: Tue, 22 Jul 2025 15:08:37 +0300 Subject: Initial template --- src/utils/frontmatter.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/utils/frontmatter.ts (limited to 'src/utils/frontmatter.ts') diff --git a/src/utils/frontmatter.ts b/src/utils/frontmatter.ts new file mode 100644 index 0000000..bf33632 --- /dev/null +++ b/src/utils/frontmatter.ts @@ -0,0 +1,50 @@ +import getReadingTime from 'reading-time'; +import { toString } from 'mdast-util-to-string'; +import { visit } from 'unist-util-visit'; +import type { RehypePlugin, RemarkPlugin } from '@astrojs/markdown-remark'; + +export const readingTimeRemarkPlugin: RemarkPlugin = () => { + return function (tree, file) { + const textOnPage = toString(tree); + const readingTime = Math.ceil(getReadingTime(textOnPage).minutes); + + if (typeof file?.data?.astro?.frontmatter !== 'undefined') { + file.data.astro.frontmatter.readingTime = readingTime; + } + }; +}; + +export const responsiveTablesRehypePlugin: RehypePlugin = () => { + return function (tree) { + if (!tree.children) return; + + for (let i = 0; i < tree.children.length; i++) { + const child = tree.children[i]; + + if (child.type === 'element' && child.tagName === 'table') { + tree.children[i] = { + type: 'element', + tagName: 'div', + properties: { + style: 'overflow:auto', + }, + children: [child], + }; + + i++; + } + } + }; +}; + +export const lazyImagesRehypePlugin: RehypePlugin = () => { + return function (tree) { + if (!tree.children) return; + + visit(tree, 'element', function (node) { + if (node.tagName === 'img') { + node.properties.loading = 'lazy'; + } + }); + }; +}; -- cgit v1.2.3