Skip to content

Commit

Permalink
fix: issues with content header id system
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Trost <[email protected]>
  • Loading branch information
galexrt committed Dec 22, 2024
1 parent fde36b5 commit 53537e9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
33 changes: 32 additions & 1 deletion app/utils/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,45 @@ import type { JSONNode } from '~~/gen/ts/resources/common/content/content';
export function jsonNodeToTocLinks(n: JSONNode): TocLink[] {
const headers: TocLink[] = [];
if (/h[1-6]/i.test(n.tag)) {
const text = getTextFromContent(n);
headers.push({
id: n.id,
depth: parseInt(n.tag.replace('h', '')),
text: n.content[0]?.text ?? n.text,
text: text,
});
}

n.content.forEach((c) => headers.push(...jsonNodeToTocLinks(c)));

return headers;
}

export function getTextFromContent(n: JSONNode): string {
if (n.text !== '') {
return n.text;
}
if (n.content.length > 0) {
const text = walkContentForText(n.content);

if (text !== '') {
return text;
}
}

return n.id;
}

function walkContentForText(ns: JSONNode[]): string {
let text = '';
for (let i = 0; i < ns.length; i++) {
const element = ns[i]!;
if (element.text === '') {
text += walkContentForText(element.content);
} else {
text += element.text;
break;
}
}

return text;
}
37 changes: 34 additions & 3 deletions gen/go/proto/resources/common/content/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"database/sql/driver"
"fmt"
"regexp"
"slices"
"strings"

Expand All @@ -27,6 +28,8 @@ const (
CommentNodeType NodeType = "comment"
)

var headerTagRegex = regexp.MustCompile(`(?i)^h[1-6]$`)

// Scan implements driver.Valuer for protobuf Content.
func (x *Content) Scan(value any) error {
switch t := value.(type) {
Expand Down Expand Up @@ -215,19 +218,35 @@ func (n *JSONNode) populateFrom(htmlNode *html.Node) error {
e = e.NextSibling
}

if strings.HasPrefix(n.Tag, "h") {
if n.Id == "" {
if len(n.Tag) == 2 && headerTagRegex.MatchString(n.Tag) {
// Either empty id or "broken" id tag
if n.Id == "" || headerTagRegex.MatchString(n.Id) {
if n.Text != "" {
n.Id = utils.SlugNoDots(fmt.Sprintf("%s-%s", n.Tag, n.Text))
} else if len(n.Content) > 0 {
n.Id = utils.SlugNoDots(fmt.Sprintf("%s-%s", n.Tag, n.Content[0].Text))
n.Id = utils.SlugNoDots(fmt.Sprintf("%s-%s", n.Tag, walkContentForText(n.Content)))
}
}
}

return nil
}

func walkContentForText(ns []*JSONNode) string {
text := ""
for i := 0; i < len(ns); i++ {
element := ns[i]
if element.Text == "" {
text += walkContentForText(element.Content)
} else {
text += element.Text
break
}
}

return text
}

func (n *JSONNode) populateTo(htmlNode *html.Node) {
if n.Tag != "" {
htmlNode.Data = n.Tag
Expand All @@ -237,6 +256,18 @@ func (n *JSONNode) populateTo(htmlNode *html.Node) {
}

if n.Id != "" {
// Make sure that headers have id
if len(n.Tag) == 2 && headerTagRegex.MatchString(n.Tag) {
// Either empty id or "broken" id tag
if n.Id == "" || headerTagRegex.MatchString(n.Id) {
if n.Text != "" {
n.Id = utils.SlugNoDots(fmt.Sprintf("%s-%s", n.Tag, n.Text))
} else if len(n.Content) > 0 {
n.Id = utils.SlugNoDots(fmt.Sprintf("%s-%s", n.Tag, walkContentForText(n.Content)))
}
}
}

htmlNode.Attr = append(htmlNode.Attr, html.Attribute{
Key: "id",
Val: n.Id,
Expand Down
3 changes: 2 additions & 1 deletion gen/go/proto/services/wiki/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ func (s *Server) UpdatePage(ctx context.Context, req *UpdatePageRequest) (*Updat
WHERE(jet.AND(
tPage.Job.EQ(jet.String(userInfo.Job)),
tPage.DeletedAt.IS_NULL(),
tPage.ParentID.IS_NULL(),
))

var ids struct {
Expand All @@ -657,7 +658,7 @@ func (s *Server) UpdatePage(ctx context.Context, req *UpdatePageRequest) (*Updat
return nil, errswrap.NewError(err, errorswiki.ErrFailedQuery)
}

if p.Job != userInfo.Job {
if p.Job != userInfo.Job && !userInfo.SuperUser {
return nil, errorswiki.ErrPageDenied
}

Expand Down

0 comments on commit 53537e9

Please sign in to comment.