Fix markdown link issue (#4849)

feat/raycast
Arik Chakma 11 months ago committed by GitHub
parent cf5dd19652
commit e0b3209dc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      src/components/TopicDetail/TopicDetail.tsx
  2. 12
      src/lib/markdown.ts
  3. 4
      src/pages/guides/[guideId].astro

@ -21,7 +21,7 @@ import type {
AllowedLinkTypes, AllowedLinkTypes,
RoadmapContentDocument, RoadmapContentDocument,
} from '../CustomRoadmap/CustomRoadmap'; } from '../CustomRoadmap/CustomRoadmap';
import { markdownToHtml } from '../../lib/markdown'; import { markdownToHtml, sanitizeMarkdown } from '../../lib/markdown';
import { cn } from '../../lib/classname'; import { cn } from '../../lib/classname';
import { Ban, FileText, X } from 'lucide-react'; import { Ban, FileText, X } from 'lucide-react';
import { getUrlParams } from '../../lib/browser'; import { getUrlParams } from '../../lib/browser';
@ -173,10 +173,11 @@ export function TopicDetail(props: TopicDetailProps) {
} else { } else {
setLinks((response as RoadmapContentDocument)?.links || []); setLinks((response as RoadmapContentDocument)?.links || []);
setTopicTitle((response as RoadmapContentDocument)?.title || ''); setTopicTitle((response as RoadmapContentDocument)?.title || '');
topicHtml = markdownToHtml(
(response as RoadmapContentDocument)?.description || '', const sanitizedMarkdown = sanitizeMarkdown(
false, (response as RoadmapContentDocument).description || '',
); );
topicHtml = markdownToHtml(sanitizedMarkdown, false);
} }
setIsLoading(false); setIsLoading(false);

@ -2,7 +2,10 @@
import MarkdownIt from 'markdown-it'; import MarkdownIt from 'markdown-it';
export function markdownToHtml(markdown: string, isInline = true): string { export function markdownToHtml(markdown: string, isInline = true): string {
const md = new MarkdownIt(); const md = new MarkdownIt({
html: true,
linkify: true,
});
if (isInline) { if (isInline) {
return md.renderInline(markdown); return md.renderInline(markdown);
@ -10,3 +13,10 @@ export function markdownToHtml(markdown: string, isInline = true): string {
return md.render(markdown); return md.render(markdown);
} }
} }
// This is a workaround for the issue with tiptap-markdown extension
// It doesn't support links with escaped brackets like this:
// \\[link\\](https://example.com) -> [link](https://example.com)
export function sanitizeMarkdown(markdown: string) {
return markdown.replace(/\\\[([^\\]+)\\\]\(([^\\]+)\)/g, '[$1]($2)');
}

@ -2,7 +2,7 @@
import GuideHeader from '../../components/GuideHeader.astro'; import GuideHeader from '../../components/GuideHeader.astro';
import MarkdownFile from '../../components/MarkdownFile.astro'; import MarkdownFile from '../../components/MarkdownFile.astro';
import BaseLayout from '../../layouts/BaseLayout.astro'; import BaseLayout from '../../layouts/BaseLayout.astro';
import { getAllGuides,GuideFileType } from '../../lib/guide'; import { getAllGuides, type GuideFileType } from '../../lib/guide';
export interface Props { export interface Props {
guide: GuideFileType; guide: GuideFileType;
@ -30,7 +30,7 @@ const { frontmatter: guideData } = guide;
> >
<GuideHeader guide={guide} /> <GuideHeader guide={guide} />
<div class='py-5 sm:py-10 max-w-[700px] mx-auto'> <div class='mx-auto max-w-[700px] py-5 sm:py-10'>
<MarkdownFile> <MarkdownFile>
<guide.Content /> <guide.Content />
</MarkdownFile> </MarkdownFile>

Loading…
Cancel
Save