summaryrefslogtreecommitdiff
path: root/src/utils/frontmatter.ts
diff options
context:
space:
mode:
authorDawid Rycerz <dawid@rycerz.xyz>2025-07-22 15:08:37 +0300
committerDawid Rycerz <dawid@rycerz.xyz>2025-07-22 15:08:37 +0300
commitfcc2f4704e39b0e69b377cc138f75027721dac22 (patch)
tree732fc94b354a26c08fba9cc9059f9c6c900182be /src/utils/frontmatter.ts
Initial template
Diffstat (limited to 'src/utils/frontmatter.ts')
-rw-r--r--src/utils/frontmatter.ts50
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';
+ }
+ });
+ };
+};