Handle link text

This commit is contained in:
2024-09-30 19:06:10 +02:00
parent b64c13442b
commit 58fe6355bc

View File

@@ -21,7 +21,26 @@ const MarkdownPreview = ({
const matches = [...rawContent.matchAll(regex)]; const matches = [...rawContent.matchAll(regex)];
for (const match of matches) { for (const match of matches) {
const [fullMatch, isImage, fileName] = match; const [fullMatch, isImage, innerContent] = match;
let fileName, displayText, heading;
// Parse the inner content
const pipeIndex = innerContent.indexOf('|');
const hashIndex = innerContent.indexOf('#');
if (pipeIndex !== -1) {
displayText = innerContent.slice(pipeIndex + 1).trim();
fileName = innerContent.slice(0, pipeIndex).trim();
} else {
displayText = innerContent;
fileName = innerContent;
}
if (hashIndex !== -1 && (pipeIndex === -1 || hashIndex < pipeIndex)) {
heading = fileName.slice(hashIndex + 1).trim();
fileName = fileName.slice(0, hashIndex).trim();
}
try { try {
const paths = await lookupFileByName(fileName); const paths = await lookupFileByName(fileName);
if (paths && paths.length > 0) { if (paths && paths.length > 0) {
@@ -29,22 +48,21 @@ const MarkdownPreview = ({
if (isImage) { if (isImage) {
result = result.replace( result = result.replace(
fullMatch, fullMatch,
`![${fileName}](${baseUrl}/files/${filePath})` `![${displayText}](${baseUrl}/files/${filePath})`
); );
} else { } else {
// Use a valid URL format that React Markdown will recognize // Include heading in the URL if present
result = result.replace( const url = heading
fullMatch, ? `${baseUrl}/internal/${encodeURIComponent(
`[${fileName}](${baseUrl}/internal/${encodeURIComponent( filePath
filePath )}#${encodeURIComponent(heading)}`
)})` : `${baseUrl}/internal/${encodeURIComponent(filePath)}`;
); result = result.replace(fullMatch, `[${displayText}](${url})`);
} }
} else { } else {
// Use a valid URL format for not found links
result = result.replace( result = result.replace(
fullMatch, fullMatch,
`[${fileName}](${baseUrl}/notfound/${encodeURIComponent( `[${displayText}](${baseUrl}/notfound/${encodeURIComponent(
fileName fileName
)})` )})`
); );
@@ -53,7 +71,9 @@ const MarkdownPreview = ({
console.error('Error looking up file:', error); console.error('Error looking up file:', error);
result = result.replace( result = result.replace(
fullMatch, fullMatch,
`[${fileName}](${baseUrl}/notfound/${encodeURIComponent(fileName)})` `[${displayText}](${baseUrl}/notfound/${encodeURIComponent(
fileName
)})`
); );
} }
} }
@@ -97,15 +117,15 @@ const MarkdownPreview = ({
), ),
a: ({ href, children }) => { a: ({ href, children }) => {
if (href.startsWith(`${baseUrl}/internal/`)) { if (href.startsWith(`${baseUrl}/internal/`)) {
const filePath = decodeURIComponent( const [filePath, heading] = decodeURIComponent(
href.replace(`${baseUrl}/internal/`, '') href.replace(`${baseUrl}/internal/`, '')
); ).split('#');
return ( return (
<a <a
href="#" href="#"
onClick={(e) => { onClick={(e) => {
e.preventDefault(); e.preventDefault();
onLinkClick(filePath); onLinkClick(filePath, heading);
}} }}
> >
{children} {children}