Skip to content

Commit

Permalink
new: Use docs path from config (#99)
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com>
  • Loading branch information
mpetrunic authored Sep 4, 2023
1 parent 5fc395a commit 10b9121
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/plugin/src/components/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import React, { useState } from 'react';
import { marked } from 'marked';
import { useDocsData } from '@docusaurus/plugin-content-docs/client';
import { useDocsVersion } from '@docusaurus/theme-common/internal';
import MDX from '@theme/MDXComponents';
import { useReflectionMap } from '../hooks/useReflectionMap';
Expand Down Expand Up @@ -229,8 +230,9 @@ export interface MarkdownProps {
export function Markdown({ content }: MarkdownProps) {
const reflections = useReflectionMap();
const version = useDocsVersion();
const docsData = useDocsData(version.pluginId);
const [ast] = useState<TokensList>(() =>
marked.lexer(replaceLinkTokens(content, reflections, version)),
marked.lexer(replaceLinkTokens(content, reflections, version, docsData.path)),
);

if (!content) {
Expand Down
10 changes: 6 additions & 4 deletions packages/plugin/src/utils/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PropVersionMetadata } from '@docusaurus/plugin-content-docs';
import type { DocusaurusConfig } from '@docusaurus/types';

Check failure on line 2 in packages/plugin/src/utils/markdown.ts

View workflow job for this annotation

GitHub Actions / Test (16)

'DocusaurusConfig' is declared but its value is never read.
import type { DeclarationReflectionMap } from '../types';

function splitLinkText(text: string): { caption: string; target: string } {
Expand Down Expand Up @@ -57,13 +58,13 @@ function replaceApiLinks(

function replaceDocLinks(
currentVersion: PropVersionMetadata,
baseUrl: string
): (match: string, content: string) => string {
return (match: string, content: string) => {
const { caption, target } = splitLinkText(content);
const version = currentVersion.version === 'current' ? 'next' : currentVersion.version;

// TODO: Handle `routeBasePath`? Something else besides "docs"?
const url = currentVersion.isLast ? `/docs/${target}` : `/docs/${version}/${target}`;

const url = currentVersion.isLast ? `${baseUrl === "/" ? "" : baseUrl }/${target}` : `${baseUrl === "/" ? "" : baseUrl }/${version}/${target}`;

return `[${caption}](${url})`;
};
Expand All @@ -75,8 +76,9 @@ export function replaceLinkTokens(
markdown: string,
reflections: DeclarationReflectionMap,
currentVersion: PropVersionMetadata,
docsBaseUrl: string
) {
return markdown
.replace(/{@(link|linkcode|linkplain|apilink)\s+([^}]+?)}/gi, replaceApiLinks(reflections))
.replace(/{@doclink\s+([^}]+?)}/gi, replaceDocLinks(currentVersion));
.replace(/{@doclink\s+([^}]+?)}/gi, replaceDocLinks(currentVersion, docsBaseUrl));
}

0 comments on commit 10b9121

Please sign in to comment.