Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dont get processed by svelte or vite or whoever, need to solve this before putti... #161

Open
github-actions bot opened this issue Dec 29, 2023 · 0 comments
Labels

Comments

@github-actions
Copy link

dont get processed by svelte or vite or whoever, need to solve this before putting it life

// node.tagName = 'enhanced:img'

// node.properties.src = ${node.properties.src}?w=1280;640;400

// node.properties.sizes = '(min-width:1920px) 1280px, (min-width:1080px) 640px, (min-width:768px) 400px'

}

}

});

};

// // TODO dont get processed by svelte or vite or whoever, need to solve this before putting it life

import * as fs from 'fs/promises';
import type { EntryType } from '$lib/types/enums';
import { join } from 'path';
import type { TocNode } from '$lib/types/post';
import { slug as slugger } from 'github-slugger';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
import rehypeHighlight from 'rehype-highlight';
import rehypeSlug from 'rehype-slug';
import rehypeStringify from 'rehype-stringify';
import { remark } from 'remark';
import remarkFrontmatter from 'remark-frontmatter';
import remarkParseFrontmatter from 'remark-parse-frontmatter';
import remarkRehype from 'remark-rehype';
import type { VFile } from 'remark-rehype/lib';
import type { Node } from 'unist';
import { visit } from 'unist-util-visit';
import type { RawEntry, RawEntryMeta } from '$lib/types/entry';

// todo maybe markdown file?
const processor = remark()
	.use(remarkFrontmatter)
	.use(remarkParseFrontmatter)
	.use(_remarkGenerateNestedToc)
	.use(remarkRehype)
	.use(rehypeSlug)
	.use(rehypeAutolinkHeadings)
	.use(_rehypeEnhanceImage)
	.use(rehypeHighlight)
	.use(rehypeStringify)
	.freeze();

export async function getRawEntries(entryType: EntryType): Promise<RawEntry[]> {
	const files = await _getFiles(entryType);
	const entries = await Promise.all(
		files.map(async (file): Promise<RawEntry> => {
			const output = processor.processSync(file);
			return {
				html: String(output.value),
				meta: output.data.frontmatter as RawEntryMeta,
				toc: output.data.toc as TocNode[]
			};
		})
	);
	return entries;
}

export async function _getFiles(entryType: EntryType): Promise<string[]> {
	const folderName =
		entryType.toLowerCase() === 'stack_entry' ? 'stack' : `${entryType.toLowerCase()}s`;
	const folderPath = join(process.cwd(), 'src', 'content', folderName);
	const fileNames = await fs.readdir(folderPath);
	return await Promise.all(
		fileNames.map(async (fileName) => {
			const filePath = join(folderPath, fileName);
			return await fs.readFile(filePath, 'utf8');
		})
	);
}

// todo maybe markdown file?
function _rehypeEnhanceImage() {
	// console.log('_enhanceImage');
	// return (tree: Node) => {
	// 	visit(tree, 'element', (node: Element) => {
	// 		if (node.tagName === 'img') {
	// 			if (!node.properties.src.endsWith('gif') || !node.properties.src.endsWith('svg')) {
	// 				// TODO dont get processed by svelte or vite or whoever, need to solve this before putting it life
	// 				// node.tagName = 'enhanced:img'
	// 				// node.properties.src = `${node.properties.src}?w=1280;640;400`
	// 				// node.properties.sizes = '(min-width:1920px) 1280px, (min-width:1080px) 640px, (min-width:768px) 400px'
	// 			}
	// 		}
	// 	});
	// };
}

// todo maybe markdown file?
function _remarkGenerateNestedToc() {
	return (tree: Node, file: VFile) => {
		const headings: { value: string; depth: number; slug: string }[] = [];
		visit(tree, 'heading', (node: TocNode) => {
			const value = (node?.children ?? []).reduce((text, child) => text + child.value, '');
			const slug = slugger(value);
			headings.push({ value, depth: node.depth, slug });
		});
		file.data.toc = _getNestedToc(headings);
	};
}

function _getNestedToc(markdownHeadings: TocNode[]): TocNode[] {
	let latestEntry: TocNode | null;
	let latestParent: TocNode | null;
	const markdownHeadingCopy = JSON.parse(JSON.stringify(markdownHeadings));
	if (markdownHeadingCopy.length <= 1) return markdownHeadingCopy;
	// TODO fix any
	const entryDepth: number = markdownHeadings.reduce((acc: number, item: TocNode) => {
		return item.depth < acc ? item.depth : acc;
	}, Number.POSITIVE_INFINITY);
	// TODO fix any
	return markdownHeadingCopy.reduce((result: TocNode[], entry: TocNode) => {
		if (latestEntry && !latestEntry.children) {
			latestEntry.children = [];
		}
		const latestEntryDepth = latestEntry?.depth || 0;
		const latestEntryChildren = latestEntry?.children || [];
		const latestParentChildren = latestParent?.children || [];
		if (entry.depth === entryDepth) {
			entry.children = [];
			result.push(entry);
			latestParent = null;
		} else if (entry.depth === latestEntryDepth + 1) {
			latestEntryChildren.push(entry);
			latestParent = latestEntry;
		} else if (entry.depth === latestEntryDepth) {
			latestParentChildren.push(entry);
		} else {
			console.error('Unexpected Toc behaviour', entry);
		}
		latestEntry = entry;
		return result;
	}, []);
}

855c10d6ffb754d60ef00e13db548a6d9654e08f

@github-actions github-actions bot added the todo label Dec 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants