|
29 | 29 | // for our use-case; DO NOT consider using this as a generic regex, or at least |
30 | 30 | // not before reading https://stackoverflow.com/a/1732454/1811501. |
31 | 31 | htmlAnchor = regexp.MustCompile(`<a\s+(?:name|id)="?([^"]+)"?\s*></a>\s*`) |
| 32 | + // relativeLink matches parts of internal links between .md documents |
| 33 | + // e.g. "](buildx_build.md)" |
| 34 | + relativeLink = regexp.MustCompile(`\]\((\.\/)?[a-z-_]+\.md(#.*)?\)`) |
32 | 35 | ) |
33 | 36 |
|
34 | 37 | // getSections returns all H2 sections by title (lowercase) |
@@ -58,6 +61,16 @@ func cleanupMarkDown(mdString string) (md string, anchors []string) { |
58 | 61 | mdString = strings.ReplaceAll(mdString, "\t", " ") |
59 | 62 | mdString = strings.ReplaceAll(mdString, "https://docs.docker.com", "") |
60 | 63 |
|
| 64 | + // Rewrite internal links, replacing relative paths with absolute path |
| 65 | + // e.g. from [docker buildx build](buildx_build.md#build-arg) |
| 66 | + // to [docker buildx build](/reference/cli/docker/buildx/build/#build-arg) |
| 67 | + mdString = relativeLink.ReplaceAllStringFunc(mdString, func(link string) string { |
| 68 | + link = strings.TrimLeft(link, "](./") |
| 69 | + link = strings.ReplaceAll(link, "_", "/") |
| 70 | + link = strings.ReplaceAll(link, ".md", "/") |
| 71 | + return "](/reference/cli/docker/" + link |
| 72 | + }) |
| 73 | + |
61 | 74 | var id string |
62 | 75 | // replace trailing whitespace per line, and handle custom anchors |
63 | 76 | lines := strings.Split(mdString, "\n") |
|
0 commit comments