From c57efee823200ea8b150e96be0ee5b701c291cbe Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sat, 17 Feb 2024 16:44:02 +0000 Subject: [PATCH] Adds reusably utility for fetching parsing yml and md --- web/src/utils/fetch-data.ts | 15 ++++++++++++ web/src/utils/parse-markdown.ts | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 web/src/utils/fetch-data.ts create mode 100644 web/src/utils/parse-markdown.ts diff --git a/web/src/utils/fetch-data.ts b/web/src/utils/fetch-data.ts new file mode 100644 index 0000000..9bb7812 --- /dev/null +++ b/web/src/utils/fetch-data.ts @@ -0,0 +1,15 @@ + +import yaml from 'js-yaml'; + +import type { AwesomePrivacy, Section, Service } from '../types/Service'; + +export const fetchData = async (): Promise => { + return await fetch('http://localhost:4321/awesome-privacy.yml') + .then((res) => res.text()) + .then((data) => yaml.load(data)) + .catch((err) => console.error(err)) as AwesomePrivacy; +} + +export const slugify = (title: string) => { + return title.toLowerCase().replace(/\s/g, '-'); +}; diff --git a/web/src/utils/parse-markdown.ts b/web/src/utils/parse-markdown.ts new file mode 100644 index 0000000..3dffdd8 --- /dev/null +++ b/web/src/utils/parse-markdown.ts @@ -0,0 +1,41 @@ +import { marked } from 'marked'; + +export const parseMarkdown = (text: string | undefined): string => { + if (!text) return ''; + + // Custom renderer + const renderer = new marked.Renderer(); + + // Override function to handle headings + renderer.heading = (text, level) => { + const escapedText = text.toLowerCase().replace(/[^\w]+/g, '-'); + return `${text}`; + }; + + // Override function to handle links + renderer.link = (href, title, text) => { + if (href.startsWith('/')) { + href = `https://github.com/Lissy93/personal-security-checklist/blob/old-version/${href}`; + } + title = title ? `title="${title}"` : ''; + return `${text}`; + }; + + // Sanitize the input to remove