Skip to content

Commit

Permalink
fix: Support relative links with titleFromH1
Browse files Browse the repository at this point in the history
  • Loading branch information
mrueg committed Mar 20, 2023
1 parent eab5655 commit 20d3d1f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func processFile(

markdown = bytes.ReplaceAll(markdown, []byte("\r\n"), []byte("\n"))

meta, markdown, err := mark.ExtractMeta(markdown)
meta, markdown, err := mark.ExtractMeta(markdown, flags.TitleFromH1)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -270,7 +270,7 @@ func processFile(
}
}

links, err := mark.ResolveRelativeLinks(api, meta, markdown, ".")
links, err := mark.ResolveRelativeLinks(api, meta, markdown, filepath.Dir(file), flags.TitleFromH1)
if err != nil {
log.Fatalf(err, "unable to resolve relative links")
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/mark/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func ResolveRelativeLinks(
meta *Meta,
markdown []byte,
base string,
titleFromH1 bool,
) ([]LinkSubstitution, error) {
matches := parseLinks(string(markdown))

Expand All @@ -42,7 +43,7 @@ func ResolveRelativeLinks(
match.hash,
)

resolved, err := resolveLink(api, base, match)
resolved, err := resolveLink(api, base, match, titleFromH1)
if err != nil {
return nil, karma.Format(err, "resolve link: %q", match.full)
}
Expand All @@ -64,12 +65,14 @@ func resolveLink(
api *confluence.API,
base string,
link markdownLink,
titleFromH1 bool,
) (string, error) {
var result string

if len(link.filename) > 0 {
filepath := filepath.Join(base, link.filename)

log.Tracef(nil, "filepath: %s", filepath)
stat, err := os.Stat(filepath)
if err != nil {
return "", nil
Expand All @@ -92,7 +95,7 @@ func resolveLink(

// This helps to determine if found link points to file that's
// not markdown or have mark required metadata
linkMeta, _, err := ExtractMeta(linkContents)
linkMeta, _, err := ExtractMeta(linkContents, titleFromH1)
if err != nil {
log.Errorf(
err,
Expand All @@ -107,6 +110,13 @@ func resolveLink(
return "", nil
}

log.Tracef(
nil,
"extracted metadata: space=%s title=%s",
linkMeta.Space,
linkMeta.Title,
)

result, err = getConfluenceLink(api, linkMeta.Space, linkMeta.Title)
if err != nil {
return "", karma.Format(
Expand Down
20 changes: 19 additions & 1 deletion pkg/mark/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var (
reHeaderPatternMacro = regexp.MustCompile(`<!-- Macro: .*`)
)

func ExtractMeta(data []byte) (*Meta, []byte, error) {
func ExtractMeta(data []byte, titleFromH1 bool) (*Meta, []byte, error) {
var (
meta *Meta
offset int
Expand Down Expand Up @@ -146,6 +146,24 @@ func ExtractMeta(data []byte) (*Meta, []byte, error) {
}
}

if titleFromH1 {
if meta == nil {
meta = &Meta{}
}

if meta.Type == "" {
meta.Type = "page"
}

if meta.ContentAppearance == "" {
meta.ContentAppearance = FullWidthContentAppearance // Default to full-width for backwards compatibility
}

if meta.Title == "" {
meta.Title = ExtractDocumentLeadingH1(data)
}
}

if meta == nil {
return nil, data, nil
}
Expand Down

0 comments on commit 20d3d1f

Please sign in to comment.