feat: handle external links in md files

This commit is contained in:
jinhojang6 2024-10-11 02:03:46 +09:00
parent 6db93e1753
commit bc632e2223
No known key found for this signature in database
GPG Key ID: 1762F21FE8B543F8

View File

@ -85,31 +85,31 @@ function parseSlugFromFrontmatter(content) {
function escapeHtmlTagSymbols(content) { function escapeHtmlTagSymbols(content) {
function replaceComparisonSymbol(text, symbol) { function replaceComparisonSymbol(text, symbol) {
// Regex to match everything, but we'll handle what to replace in the callback // Regex to match everything, but we'll handle what to replace in the callback
let regex = /(```[\s\S]+?```|`[^`]*`)|(<)/g; let regex = /(```[\s\S]+?```|`[^`]*`)|(<)/g
let replacementString = "&lt;"; let replacementString = '&lt;'
if (symbol === ">") { if (symbol === '>') {
regex = /(```[\s\S]+?```|`[^`]*`)|(>)/g; regex = /(```[\s\S]+?```|`[^`]*`)|(>)/g
replacementString = "&gt;"; replacementString = '&gt;'
} }
return text.replace(regex, function(match, code, lessThan) { return text.replace(regex, function(match, code, lessThan) {
if (code) { if (code) {
// It's a code segment, return it unchanged // It's a code segment, return it unchanged
return code; return code
} else if (lessThan) { } else if (lessThan) {
// It's a '<' outside of code blocks, replace with '&lt;' // It's a '<' outside of code blocks, replace with '&lt;'
return replacementString; return replacementString
} }
// Default return (though practically unused in this setup) // Default return (though practically unused in this setup)
return match; return match
}); })
} }
content = replaceComparisonSymbol(content, "<") content = replaceComparisonSymbol(content, '<')
content = replaceComparisonSymbol(content, ">") content = replaceComparisonSymbol(content, '>')
return content; return content
} }
function unescapeHtmlComments(htmlString) { function unescapeHtmlComments(htmlString) {
@ -118,9 +118,15 @@ function unescapeHtmlComments(htmlString) {
function updateMarkdownLinksToExcludeMD(content) { function updateMarkdownLinksToExcludeMD(content) {
function replaceLinks(match, p1, p2, p3) { function replaceLinks(match, p1, p2, p3) {
let url = p2.replace(/\.md$/, ''); // Remove .md extension from URL // Skip if the link is an external link (starts with 'http://' or 'https://')
let anchor = p3.replace(/^\//, ''); // Remove preceding '/' from anchor if exists if (match.includes('http://') || match.includes('https://')) {
return `[${p1}](${url}${anchor ? '#' + anchor : ''})`; return match
}
let url = p2.replace(/\.md$/, '') // Remove .md extension from URL
let anchor = p3.replace(/^\//, '') // Remove preceding '/' from anchor if exists
return `[${p1}](${url}${anchor ? '#' + anchor : ''})`
} }
const regex = /\[((?:(?!\]).)+)\]\(([^)]*?\.md)(?:\/#|\/#)?([^)]*)\)/g const regex = /\[((?:(?!\]).)+)\]\(([^)]*?\.md)(?:\/#|\/#)?([^)]*)\)/g
@ -129,7 +135,7 @@ function updateMarkdownLinksToExcludeMD(content) {
} }
export function vacMarkdownToDocusaurusMarkdown(fileContent) { export function vacMarkdownToDocusaurusMarkdown(fileContent) {
let convertedContent = fileContent; let convertedContent = fileContent
// Remove 'tags' line from frontmatter because the format is wrong // Remove 'tags' line from frontmatter because the format is wrong
convertedContent = convertedContent.replace(/tags:.*\n?/, '') convertedContent = convertedContent.replace(/tags:.*\n?/, '')
@ -151,7 +157,7 @@ export function vacMarkdownToDocusaurusMarkdown(fileContent) {
convertedContent = updateMarkdownLinksToExcludeMD(convertedContent) convertedContent = updateMarkdownLinksToExcludeMD(convertedContent)
return convertedContent; return convertedContent
} }
export function adjustPathForMarkdown(filePath) { export function adjustPathForMarkdown(filePath) {