fix parsing issues

This commit is contained in:
jackyzha0 2021-07-17 11:39:53 -04:00
parent 5879c08972
commit 6155ce71dc
4 changed files with 24 additions and 9 deletions

View File

@ -1,12 +1,15 @@
# Obsidian Link Scrapper
GitHub action and binary to scrape [Obsidian](http://obsidian.md/) vault for links and exposes them as a `.yml` file for easy consumption by [Hugo](https://gohugo.io/).
## Installation
`go install github.com/jackyzha0/hugo-obsidian`
### Example Usage
## Example Usage (Binary)
Read Markdown from the `/content` folder and place the resulting `linkIndex.yaml` into `/data`
```shell
# Installation
go install github.com/jackyzha0/hugo-obsidian
# Run
hugo-obsidian -input=content -output=data
```
```
## Example Usage (GitHub Action)

13
main.go
View File

@ -28,6 +28,10 @@ func trim(source, prefix, suffix string) string {
return strings.TrimPrefix(strings.TrimSuffix(source, suffix), prefix)
}
func hugoPathTrim(source string) string {
return strings.TrimSuffix(strings.TrimSuffix(source, "/index"), "_index")
}
// parse single file for links
func parse(dir, pathPrefix string) []Link {
// read file
@ -42,8 +46,8 @@ func parse(dir, pathPrefix string) []Link {
for text, target := range md.GetAllLinks(string(bytes)) {
fmt.Printf(" %s\n", trim(target, pathPrefix, ".md"))
links = append(links, Link{
Source: trim(dir, pathPrefix, ".md"),
Target: target,
Source: hugoPathTrim(trim(dir, pathPrefix, ".md")),
Target: strings.Split(target, "#")[0],
Text: text,
})
}
@ -95,8 +99,9 @@ func index(links []Link) (index Index) {
}
// backlink (only if internal)
if val, ok := backlinkMap[l.Target]; ok {
val = append(val, bl)
if _, ok := backlinkMap[l.Target]; ok {
backlinkMap[l.Target] = append(backlinkMap[l.Target], bl)
} else {
backlinkMap[l.Target] = []Link{bl}
}

View File

@ -0,0 +1,7 @@
<script>
const links = [
{{- range $.Site.Data.linkIndex.links -}}
{}
{{- end -}}
]
</script>

View File