feat: moving to IsTextFile for file type detection

This commit is contained in:
Lucas Oskorep
2023-03-22 19:10:19 -04:00
parent 67c9b248dd
commit 30b0785fae
3 changed files with 16 additions and 21 deletions

View File

@@ -7,11 +7,11 @@ import (
"os"
"path/filepath"
"regexp"
"strings"
"github.com/kovetskiy/mark/pkg/confluence"
"github.com/reconquest/karma-go"
"github.com/reconquest/pkg/log"
"golang.org/x/tools/godoc/util"
)
type LinkSubstitution struct {
@@ -44,7 +44,6 @@ func ResolveRelativeLinks(
match.filename,
match.hash,
)
resolved, err := resolveLink(api, base, match, spaceFromCli, titleFromH1)
if err != nil {
return nil, karma.Format(err, "resolve link: %q", match.full)
@@ -73,10 +72,10 @@ func resolveLink(
var result string
if len(link.filename) > 0 {
filePath := filepath.Join(base, link.filename)
filepath := filepath.Join(base, link.filename)
log.Tracef(nil, "filePath: %s", filePath)
stat, err := os.Stat(filePath)
log.Tracef(nil, "filepath: %s", filepath)
stat, err := os.Stat(filepath)
if err != nil {
return "", nil
}
@@ -85,9 +84,14 @@ func resolveLink(
return "", nil
}
linkContents, err := os.ReadFile(filePath)
linkContents, err := os.ReadFile(filepath)
if !util.IsText(linkContents) {
return "", nil
}
if err != nil {
return "", karma.Format(err, "read file: %s", filePath)
return "", karma.Format(err, "read file: %s", filepath)
}
linkContents = bytes.ReplaceAll(
@@ -98,15 +102,12 @@ func resolveLink(
// This helps to determine if found link points to file that's
// not markdown or have mark required metadata
if !IsMarkdownFile(filepath.Ext(filePath)) {
return "", nil
}
linkMeta, _, err := ExtractMeta(linkContents, spaceFromCli, titleFromH1)
if err != nil {
log.Errorf(
err,
"unable to extract metadata from %q; ignoring the relative link",
filePath,
filepath,
)
return "", nil
@@ -128,7 +129,7 @@ func resolveLink(
return "", karma.Format(
err,
"find confluence page: %s / %s / %s",
filePath,
filepath,
linkMeta.Space,
linkMeta.Title,
)
@@ -206,12 +207,3 @@ func getConfluenceLink(
return link, nil
}
func IsMarkdownFile(extension string) bool {
switch strings.ToLower(extension) {
case
"md":
return true
}
return false
}