Skip to content

Commit 0ec5edc

Browse files
Add handling headers in relative links and update relevant tests
1 parent a5a54dc commit 0ec5edc

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

pkg/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const (
1919
linkPattern = `\[([^\]]*)\]\(([^)]*)\)|\bhttps?://\S*\b`
2020
headerPattern = `^#{1,6}? (.*)`
2121
httpsPattern = `^https?://`
22-
hashPattern = `#(.*)`
22+
hashPattern = `^#(.*)`
2323
)
2424

2525
func (p *Parser) Links(markdown, dirPath string) Links {

pkg/validation.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,22 @@ func (*Validation) externalLink(link Link) (Link, error) {
158158
return link, nil
159159
}
160160

161-
func (*Validation) internalLink(link Link) (Link, error) {
161+
func (v *Validation) internalLink(link Link) (Link, error) {
162162
if link.TypeOf != InternalLink {
163163
return link, nil
164164
}
165165

166-
if err := fileExists(link.AbsPath); err == nil {
166+
splitted := strings.Split(link.AbsPath, "#")
167+
168+
if err := fileExists(splitted[0]); err == nil {
167169
link.Result.Status = true
170+
171+
if len(splitted) == 2 {
172+
if !v.isHashInFile(splitted[0], splitted[1]) {
173+
link.Result.Status = false
174+
link.Result.Message = "The specified header doesn't exist in file"
175+
}
176+
}
168177
} else {
169178
link.Result.Status = false
170179
link.Result.Message = "The specified file doesn't exist"
@@ -186,11 +195,22 @@ func (*Validation) hashInternalLink(link Link, headers Headers) (Link, error) {
186195
closestHeader = strings.ToLower(closestHeader)
187196

188197
link.Result.Status = false
189-
if closestHeader != "" {
190-
link.Result.Message = fmt.Sprintf("The specified header doesn't exist in file. Did you mean about #%s?", closestHeader)
191-
} else {
192-
link.Result.Message = "The specified header doesn't exist in file"
193-
}
198+
link.Result.Message = "The specified header doesn't exist in file"
199+
//if closestHeader != "" {
200+
// link.Result.Message = fmt.Sprintf("The specified header doesn't exist in file. Did you mean about #%s?", closestHeader)
201+
//} else {
202+
// link.Result.Message = "The specified header doesn't exist in file"
203+
//}
194204
}
195205
return link, nil
196206
}
207+
208+
func (*Validation) isHashInFile(file, header string) bool {
209+
markdown, err := readMarkdown(file)
210+
if err != nil {
211+
return false
212+
}
213+
214+
parser := Parser{}
215+
return headerExists(header, parser.Headers(markdown))
216+
}

pkg/validation_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ func TestValidation(t *testing.T) {
7272
AbsPath: "test-markdowns/sub_path/invalid.md",
7373
TypeOf: InternalLink,
7474
},
75+
Link{
76+
AbsPath: "test-markdowns/external_links.md#first-header",
77+
TypeOf: InternalLink,
78+
},
79+
Link{
80+
AbsPath: "test-markdowns/external_links.md#unknown-header",
81+
TypeOf: InternalLink,
82+
},
7583
}
7684

7785
expected := []Link{
@@ -104,6 +112,21 @@ func TestValidation(t *testing.T) {
104112
Message: "The specified file doesn't exist",
105113
},
106114
},
115+
Link{
116+
AbsPath: "test-markdowns/external_links.md#first-header",
117+
TypeOf: InternalLink,
118+
Result: LinkResult{
119+
Status: true,
120+
},
121+
},
122+
Link{
123+
AbsPath: "test-markdowns/external_links.md#unknown-header",
124+
TypeOf: InternalLink,
125+
Result: LinkResult{
126+
Status: false,
127+
Message: "The specified header doesn't exist in file",
128+
},
129+
},
107130
}
108131

109132
valid := &Validation{}

0 commit comments

Comments
 (0)