diff options
| author | Dawid Rycerz <dawid@rycerz.xyz> | 2025-07-22 15:08:37 +0300 |
|---|---|---|
| committer | Dawid Rycerz <dawid@rycerz.xyz> | 2025-07-22 15:08:37 +0300 |
| commit | fcc2f4704e39b0e69b377cc138f75027721dac22 (patch) | |
| tree | 732fc94b354a26c08fba9cc9059f9c6c900182be /src/utils/frontmatter.ts | |
Initial template
Diffstat (limited to 'src/utils/frontmatter.ts')
| -rw-r--r-- | src/utils/frontmatter.ts | 50 |
1 files changed, 50 insertions, 0 deletions
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'; + } + }); + }; +}; |
