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 # 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/). 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 ## Example Usage (Binary)
`go install github.com/jackyzha0/hugo-obsidian`
### Example Usage
Read Markdown from the `/content` folder and place the resulting `linkIndex.yaml` into `/data` Read Markdown from the `/content` folder and place the resulting `linkIndex.yaml` into `/data`
```shell ```shell
# Installation
go install github.com/jackyzha0/hugo-obsidian
# Run
hugo-obsidian -input=content -output=data 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) 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 // parse single file for links
func parse(dir, pathPrefix string) []Link { func parse(dir, pathPrefix string) []Link {
// read file // read file
@ -42,8 +46,8 @@ func parse(dir, pathPrefix string) []Link {
for text, target := range md.GetAllLinks(string(bytes)) { for text, target := range md.GetAllLinks(string(bytes)) {
fmt.Printf(" %s\n", trim(target, pathPrefix, ".md")) fmt.Printf(" %s\n", trim(target, pathPrefix, ".md"))
links = append(links, Link{ links = append(links, Link{
Source: trim(dir, pathPrefix, ".md"), Source: hugoPathTrim(trim(dir, pathPrefix, ".md")),
Target: target, Target: strings.Split(target, "#")[0],
Text: text, Text: text,
}) })
} }
@ -95,8 +99,9 @@ func index(links []Link) (index Index) {
} }
// backlink (only if internal) // 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 { } else {
backlinkMap[l.Target] = []Link{bl} backlinkMap[l.Target] = []Link{bl}
} }

View File

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

View File