在 GitLab 页面上发布 Docusaurus 站点时如何修复内部 link 问题

How to fix internal link issues when publishing a Docusaurus site on GitLab pages

在我的 Docusaurus 项目中,我的内部 links 在我的本地环境中工作,但是当我推送到 GitLab 时,它们不再工作。它没有用新的文档标题替换原始文档标题,而是将其添加到末尾的 url ('https://username.io/test-site/docs/overview/add-a-category.html')。我查看了我的配置文件,但我不明白为什么会这样。

我尝试更新页面前端的 ID,并确保它与 sidebars.json 文件中的 ID 匹配。我还添加了 customDocsPath 并将其设置为 'docs/' 在配置文件中,尽管这应该是默认设置。

---
id: "process-designer-overview"
title: "Process Designer Overview"
sidebar_label: "Overview"
---
# Process Designer

The Process Designer is a collaborative business process modeling and 
design workspace for the business processes, scenarios, roles and tasks 
that make up governed data processes.

Use the Process Designer to:

 - [Add a Category](add-a-category.html)
 - [Add a Process or Scenario](Add%20a%20Process%20or%20Scenario.html)
 - [Edit a Process or Scenario](Edit%20a%20Process%20or%20Scenario.html)

我更新了在括号中添加类别 link 到 md 扩展,但这破坏了我本地的 link 并且它在 GitLab 上仍然不起作用。我希望当用户单击 link 时,它会将 url 中的文档标题替换为新的文档标题('https://username.gitlab.io/docs/add-a-category.html') but instead it just tacks it on to the end ('https://username.gitlab.io/docs/process-designer-overview/add-a-category.html'),因此 link已损坏,因为那不是文档所在的位置。

我的链接有几个问题。首先,我使用 Pandoc 将这些文件从 html 转换为 markdown,并且没有添加前面的内容——而是依靠文件名将我的文件连接到侧边栏。这很好,除了几乎所有文件名中都有空格,您可以在上面的代码示例中看到这一点。这导致了真正的问题,所以我找到了一个 Bash 脚本来用下划线替换我文件名中的所有空格,但现在我所有的链接都被破坏了。我在我的代码编辑器中通过搜索和替换更新了文件中的所有链接,将“%20”替换为“_”。我还需要用“.md”替换“.html”扩展名,否则我的项目将无法在本地运行。同样,我在我的代码编辑器中通过搜索和替换来做到这一点。

最后,我最终添加了前面的内容,否则我的边栏标题都被下划线覆盖了。因为我要处理 90 个文件,所以我不想手动执行此操作。我找了一会儿,找到了一个 great gist by thebearJew 并对其进行了调整,使其采用文件名并将其添加为 id,将第一个标题添加为标题和 sidebar_label,因为作为它恰好适用于我们的项目。这是我在网上找到的 Bash 脚本,如果感兴趣,可以将文件名中的空格转换为下划线:

find  -name "* *.md" -type f -print0 | \
  while read -d $'[=10=]' f; do mv -v "$f" "${f// /_}"; done

如果其他人有类似的设置并且不想更新大量带有前言的文件,这是我最终得到的脚本:

# Given a file path as an argument
# 1. get the file name
# 2. prepend template string to the top of the source file
# 3. resave original source file

# command: find . -name "*.md" -print0 | xargs -0 -I file ./prepend.sh file

filepath=""
file_name=$("basename" -a "$filepath")

# Getting the file name (title)
md='.md'
title=${file_name%$md}
heading=$(grep -r "^# \b" ~/Documents/docs/$title.md)
heading1=${heading#*\#}

# Prepend front-matter to files
TEMPLATE="---
id: $title
title: $heading1
sidebar_label: $heading1
---
"
echo "$TEMPLATE" | cat - "$filepath" > temp && mv temp "$filepath"