Skip to content

Commit

Permalink
Merge pull request #8 from perkinsben/aliases-in-markdown
Browse files Browse the repository at this point in the history
Adding support for aliases.md as an alternative to aliases.yml
  • Loading branch information
perkinsben authored Nov 16, 2020
2 parents 72920ee + 027ecb0 commit 8d50d5f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
32 changes: 27 additions & 5 deletions forward_linker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ And the following text in your clipboard to import:
After executing the script, it would be replaced with the following:

```[[Psychology]] is the [[Science]] of human [[Behavior|behavior]].```
---

## Aliases
# Aliases

You can use a file named aliases.yml in your vault root to broaden the title matching in your text to include aliases.

Expand Down Expand Up @@ -83,11 +84,25 @@ Input text:
Output text:
```In order to properly [[problem solving|problem solve]], you should wear your [[problem solving|Problem Solving]] Hat.```

## Notes
## aliases.md

This tool leverages an opinionated use case of Obsidian. Those who use literal text titles to identify their notes will find more utility than those who don't (eg. the zettelkasten folks), but using the aliases file strategically can handle a wide range of matching scenarios. If you have duplicated titles in your vault (ie. Obsidian prefixed them with a folder name due to ambiguity) you’ll only get links to the original (unqualified) page with that title.
In addition to defining aliases in aliases.yml, the tool also supports using aliases.md (also placed in vault root) as an alternative. If both files are present, aliases.md takes precedence. The format of both files is the same (described above), except aliases.md supports wikilinks in titles (see below).

Using aliases.md provides some additional features:
- The file can be opened in Obsidian and edited on-the-fly, repositioned, etc. (best viewed in edit mode)
- The page titles can be linked, eg.:
```
[[problem solving]]:
- solving problems
- problem solve
```
- When page titles are linked, they will show in the backlinks section (linked mentions). This gives visibility into the aliases defined for a page being viewed (works best with 'Show more context' enabled in backlinks section).

Spec04 wrote a python script that generates the aliases.yml file from tags within your note files: [Obsidian Alias File Generator](https://github.com/Spec04/obs_alias_generator)
You may wish to exclude the aliases file itself from being linked during the auto-linking process by adding the following to aliases.md:
```
aliases:
-
```

---
# Obsidian Unlinker
Expand Down Expand Up @@ -147,4 +162,11 @@ RunWait, cmd.exe /c python obs-unlinkr.py c:\path\to\your\vault, c:\scripts\obs_
Send ^v
```

You'll have to modify the paths to match your own, and may need to add Python to your Windows %PATH%.
You'll have to modify the paths to match your own, and may need to add Python to your Windows %PATH%.

---
# Notes

This tool leverages an opinionated use case of Obsidian. Those who use literal text titles to identify their notes will find more utility than those who don't (eg. the zettelkasten folks), but using the aliases file strategically can handle a wide range of matching scenarios. If you have duplicated titles in your vault (ie. Obsidian prefixed them with a folder name due to ambiguity) you’ll only get links to the original (unqualified) page with that title.

Spec04 wrote a python script that generates the aliases.yml file from tags within your note files: [Obsidian Alias File Generator](https://github.com/Spec04/obs_alias_generator)
32 changes: 22 additions & 10 deletions forward_linker/obs-linkr.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,37 @@ def link_content(content):
#print(page_title)
page_titles.append(page_title)

# we'll also check for an aliases file and load that
# we'll also check for an aliases file and load that (.md -> .yml)
# we pivot (invert) the dict for lookup purposes
aliases_file = obsidian_home + "/aliases.yml"
if os.path.isfile(aliases_file):
aliases_file = obsidian_home + "/aliases"

if os.path.isfile(aliases_file + ".md"):
aliases_file += ".md"
elif os.path.isfile(aliases_file + ".yml"):
aliases_file += ".yml"
else:
aliases_file = None

if aliases_file:
with open(aliases_file, 'r') as stream:
try:
aliases = yaml.full_load(stream)
# this line injects quotes around wikilinks so that yaml parsing won't fail
# we remove them later, so they are only a temporary measure
aliases_txt = stream.read().replace("[[", "\"[[").replace("]]", "]]\"")
aliases = yaml.full_load(aliases_txt)

for title in aliases:
#print(title)
for title in aliases:
if aliases[title]:
for alias in aliases[title]:
# strip out wikilinks and quotes from title if present
sanitized_title = title.replace("[[", "").replace("]]", "").replace("\"", "")
if alias:
page_aliases[alias] = title
page_aliases[alias] = sanitized_title
else:
# empty entry will signal to ignore page title in matching
print("Empty alias (will be ignored): " + title)
if title in page_titles:
page_titles.remove(title)
print("Empty alias (will be ignored): " + sanitized_title)
if sanitized_title in page_titles:
page_titles.remove(sanitized_title)
#print(page_aliases)
except yaml.YAMLError as exc:
print(exc)
Expand Down

0 comments on commit 8d50d5f

Please sign in to comment.