Commit c57efee823

c57efee823200ea8b150e96be0ee5b701c291cbe

parent: 5def0fe53f

Unsigned

Alicia Sykes <alicia@omg.lol> · 2024-02-17 16:44 UTC

Adds reusably utility for fetching parsing yml and md

Layout: unified · split

web/src/utils/fetch-data.ts added +15
@@ -0,0 +1,15 @@
1
2import yaml from 'js-yaml';
3
4import type { AwesomePrivacy, Section, Service } from '../types/Service';
5
6export const fetchData = async (): Promise<AwesomePrivacy> => {
7 return await fetch('http://localhost:4321/awesome-privacy.yml')
8 .then((res) => res.text())
9 .then((data) => yaml.load(data))
10 .catch((err) => console.error(err)) as AwesomePrivacy;
11}
12
13export const slugify = (title: string) => {
14 return title.toLowerCase().replace(/\s/g, '-');
15};
web/src/utils/parse-markdown.ts added +41
@@ -0,0 +1,41 @@
1import { marked } from 'marked';
2
3export const parseMarkdown = (text: string | undefined): string => {
4 if (!text) return '';
5
6 // Custom renderer
7 const renderer = new marked.Renderer();
8
9 // Override function to handle headings
10 renderer.heading = (text, level) => {
11 const escapedText = text.toLowerCase().replace(/[^\w]+/g, '-');
12 return `<h${level} id="${escapedText}">${text}</h${level}>`;
13 };
14
15 // Override function to handle links
16 renderer.link = (href, title, text) => {
17 if (href.startsWith('/')) {
18 href = `https://github.com/Lissy93/personal-security-checklist/blob/old-version/${href}`;
19 }
20 title = title ? `title="${title}"` : '';
21 return `<a href="${href}" ${title} target="_blank" rel="noopener noreferrer">${text}</a>`;
22 };
23
24 // Sanitize the input to remove <script> tags
25 const sanitizeHtml = (html: string): string => {
26 return html.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
27 };
28
29 // Configure marked with the custom renderer
30 marked.use({ renderer });
31
32 // Parse the markdown, then sanitize the HTML to remove <script> tags
33 const rawHtml = marked.parse(text, { async: false}) as string;
34 const sanitizedHtml = sanitizeHtml(rawHtml);
35
36 return sanitizedHtml;
37};
38
39export const formatLink = (link: string) => {
40 return (link || '').replace(/^(https?:\/\/)?(www\.)?/, '').replace(/\/+$/, '');
41};