Canonical path adherence (#10)
Reviewed-on: https://gitea.westgate.pw/ShadowsOverWestgate/sow-tools/pulls/10 Co-authored-by: vickydotbat <vickydotbat@tutamail.com> Co-committed-by: vickydotbat <vickydotbat@tutamail.com>
This commit is contained in:
@@ -66,7 +66,7 @@ type deployResult struct {
|
||||
type wikiDeployPage struct {
|
||||
PageID string
|
||||
Title string
|
||||
PublicSlug string
|
||||
PublicPath string
|
||||
Namespace string
|
||||
Content string
|
||||
Hash string
|
||||
@@ -333,7 +333,7 @@ func collectLocalPages(sourceDir string, namespaces []string) (map[string]wikiDe
|
||||
pages[pageID] = wikiDeployPage{
|
||||
PageID: pageID,
|
||||
Title: extractPageTitle(content, pageID),
|
||||
PublicSlug: extractWikiPagePublicSlug(content),
|
||||
PublicPath: extractWikiPagePublicPath(content),
|
||||
Namespace: namespace,
|
||||
Content: content,
|
||||
Hash: computeManagedHash(content),
|
||||
@@ -636,10 +636,10 @@ func fetchNodeBBWikiPageMapping(page wikiDeployPage, cid int, remotePage nodeBBW
|
||||
}
|
||||
|
||||
func matchExistingNodeBBPage(page wikiDeployPage, remotePages []nodeBBWikiPage) (nodeBBWikiPage, bool) {
|
||||
if page.PublicSlug != "" {
|
||||
if page.PublicPath != "" {
|
||||
for _, candidate := range remotePages {
|
||||
for _, slug := range []string{nodeBBPathLeaf(candidate.Slug), nodeBBPathLeaf(candidate.WikiPath)} {
|
||||
if slugifyWikiTitle(slug) == page.PublicSlug {
|
||||
for _, path := range []string{candidate.WikiPath, candidate.Slug} {
|
||||
if normalizeWikiPath(path) == normalizeWikiPath(page.PublicPath) {
|
||||
return candidate, true
|
||||
}
|
||||
}
|
||||
@@ -653,12 +653,12 @@ func matchExistingNodeBBPage(page wikiDeployPage, remotePages []nodeBBWikiPage)
|
||||
}
|
||||
desiredSlugs := map[string]struct{}{}
|
||||
for title := range desiredTitles {
|
||||
if slug := slugifyWikiTitle(title); slug != "" {
|
||||
if slug := strings.ToLower(canonicalWikiSegment(title)); slug != "" {
|
||||
desiredSlugs[slug] = struct{}{}
|
||||
}
|
||||
}
|
||||
for _, slug := range []string{pageIDLeaf(page.PageID), strings.ReplaceAll(pageIDRemainder(page.PageID), ":", "-")} {
|
||||
if normalized := slugifyWikiTitle(slug); normalized != "" {
|
||||
if normalized := strings.ToLower(canonicalWikiSegment(slug)); normalized != "" {
|
||||
desiredSlugs[normalized] = struct{}{}
|
||||
}
|
||||
}
|
||||
@@ -670,7 +670,7 @@ func matchExistingNodeBBPage(page wikiDeployPage, remotePages []nodeBBWikiPage)
|
||||
}
|
||||
}
|
||||
for _, slug := range []string{nodeBBPathLeaf(candidate.Slug), nodeBBPathLeaf(candidate.WikiPath)} {
|
||||
if _, ok := desiredSlugs[slugifyWikiTitle(slug)]; ok {
|
||||
if _, ok := desiredSlugs[strings.ToLower(canonicalWikiSegment(slug))]; ok {
|
||||
return candidate, true
|
||||
}
|
||||
}
|
||||
@@ -718,6 +718,20 @@ func slugifyWikiTitle(value string) string {
|
||||
return slugifyWikiText(value, "")
|
||||
}
|
||||
|
||||
func normalizeWikiPath(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
value = strings.TrimPrefix(value, "/wiki/")
|
||||
value = strings.TrimPrefix(value, "wiki/")
|
||||
parts := []string{}
|
||||
for _, part := range strings.Split(value, "/") {
|
||||
canonical := canonicalWikiSegment(part)
|
||||
if canonical != "" {
|
||||
parts = append(parts, strings.ToLower(canonical))
|
||||
}
|
||||
}
|
||||
return strings.Join(parts, "/")
|
||||
}
|
||||
|
||||
func loadDeployManifest(path string) wikiDeployManifest {
|
||||
doc := wikiDeployManifest{Version: "nodebb-v1", Pages: map[string]wikiDeployManifestPage{}}
|
||||
raw, err := os.ReadFile(path)
|
||||
@@ -910,9 +924,9 @@ func renderArchivedWikiPage(pageID, title string) string {
|
||||
}, "\n"))
|
||||
}
|
||||
|
||||
func extractWikiPagePublicSlug(content string) string {
|
||||
func extractWikiPagePublicPath(content string) string {
|
||||
const pageMarker = "sow-topdata-wiki:page="
|
||||
const slugField = "wiki_slug="
|
||||
const pathField = "public_path="
|
||||
start := strings.Index(content, pageMarker)
|
||||
if start < 0 {
|
||||
return ""
|
||||
@@ -922,15 +936,15 @@ func extractWikiPagePublicSlug(content string) string {
|
||||
return ""
|
||||
}
|
||||
marker := content[start : start+end]
|
||||
fieldStart := strings.Index(marker, slugField)
|
||||
fieldStart := strings.Index(marker, pathField)
|
||||
if fieldStart < 0 {
|
||||
return ""
|
||||
}
|
||||
value := marker[fieldStart+len(slugField):]
|
||||
value := marker[fieldStart+len(pathField):]
|
||||
if fields := strings.Fields(value); len(fields) > 0 {
|
||||
slug := strings.TrimSpace(fields[0])
|
||||
if slug != "" && slug == slugifyWikiTitle(slug) {
|
||||
return slug
|
||||
path := strings.Trim(strings.TrimSpace(fields[0]), `"`)
|
||||
if path != "" && canonicalWikiPath(path) == path {
|
||||
return path
|
||||
}
|
||||
}
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user