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

1
go.mod
View File

@ -12,6 +12,7 @@ require (
github.com/reconquest/pkg v1.3.0 github.com/reconquest/pkg v1.3.0
github.com/reconquest/regexputil-go v0.0.0-20160905154124-38573e70c1f4 github.com/reconquest/regexputil-go v0.0.0-20160905154124-38573e70c1f4
github.com/stretchr/testify v1.8.1 github.com/stretchr/testify v1.8.1
golang.org/x/tools v0.7.0
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )

2
go.sum
View File

@ -43,6 +43,8 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/zazab/zhash v0.0.0-20210630080733-6e809466f8d3 h1:BhVaeQJc3xalHGONn215FylzuxdQBIT3d/aRjDg4nXQ= github.com/zazab/zhash v0.0.0-20210630080733-6e809466f8d3 h1:BhVaeQJc3xalHGONn215FylzuxdQBIT3d/aRjDg4nXQ=
github.com/zazab/zhash v0.0.0-20210630080733-6e809466f8d3/go.mod h1:NtepZ8TEXErPsmQDMUoN72f8aIy4+xNinSJ3f1giess= github.com/zazab/zhash v0.0.0-20210630080733-6e809466f8d3/go.mod h1:NtepZ8TEXErPsmQDMUoN72f8aIy4+xNinSJ3f1giess=
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

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