gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
73.58k stars 7.39k forks source link

Add support for page backlinks (e.g. `.GetBacklinks`) #8077

Open gcushen opened 3 years ago

gcushen commented 3 years ago

Problem

As far as I can tell, there is currently no user friendly and efficient way to get the set of internal pages that link to a page (aka backlinks) via Hugo's Go templating.

Internal backlinks are really useful to those using Hugo in academia and for research and note taking. It's become one of the top selling points used by popular note taking apps such as Notion and Roam Research.

There is also demand in the community for such capability, for example:

Possible high-level solution

Expose a .GetBacklinks page option which provides a list of Page instances (similar to .GetTerms output) that link to the current page.

HughP commented 3 years ago

Ok, so I'm not sure what you mean by "back links". I did read the the linked articles... This is only for links inside of the blog, not pingbacks like wordpress, where if another blog mentions your article then you generate a link saying they were talking about your blogpost.

Can we get a few use cases of how people might be interested to use this? I use the tags feature and the related content features. See related content here: https://hughandbecky.us/Becky-CV/publication/2015-narrative-uses-of-utmatin-bare-verb

davidsneighbour commented 3 years ago

Use cases are probably always wiki-like features, where you show which page links to what page. Using tags is certainly a solution, but it will make you have to edit all pages that your new page links to, to add a backlink. While having this done internally would be nice, it might also slow the build process down (for people that do not require that feature). I am pretty sure this can be done with some kind of .scratch functionality.

iphysresearch commented 3 years ago

Love the new feature "backlinks" or "bidirectional links"! As a heavy fan of Roam research, I am truly looking forward to this!

It's all related to zettelkasten —— knowledge management and note-taking method used in research and study.

bep commented 3 years ago

I understand the "usefulness", but this is in my head currently very hard to do. For this to work you would probably need to render all content in 1 thread before you can do anything else -- which would slow down the build a whole lot.

NightMachinery commented 3 years ago

@bep If it is an opt-in feature, the slowdown is tolerable. One can turn it off when one is doing experiments requiring fast reload, and turn it back on for the production version. Anyhow, a slow thing is better than nothing.

lucasew commented 3 years ago

It can be opt in via the configuration file.

A way to index this is to traverse the files looking for internal link references. If a file changes you can just update the index of that file.

It will require a warm-up on setup to index and because it's opt in if you don't use you can just disable with no penalty.

sjgknight commented 2 years ago

@gcushen I'm interested in this but not competent to implement. In looking around I've found two people discussing ways of doing this https://gabrielleearnshaw.medium.com/implementing-backlinks-in-a-hugo-website-e548d3d8f0e0 and https://seds.nl/notes/export_org_roam_backlinks_with_gohugo/ (I'm going to play, but I'm not likely to be able to implement). I can imagine academics e.g. being interested in linking projects<->publications<->authors<->news.
Taxonomies/tags can do some of this I think, but it's not quite the same. @apreshill also has an "on this page" which shows the headings for a page, which I assume could be used to show the links https://github.com/rbind/apreshill/search?q=%22on+this+page%22&type= ? Apologies this is just a a link dump .

lucasew commented 2 years ago

The problem of using taxonomies (tag is one of the default taxonomies) is that you can't get from the content. It's all defined on the frontmatter. Not so convenient if you are using your obsidian vault. I've made a template that uses an obsidian extension to dump the relations [1]. It's not the ideal approach because I am having even more strong cpu spikes each time obsidian exports the metadata because my obsidian vault keeps growing. I am getting almost 10s for the cold build with 1052 pages. My laptop gets pretty warm so I limit via GOMAXPROCS the amount of cores hugo can use to half of them. For incremental builds it seems to work pretty nice but when I am typing something it starts screaming the fans so a way to defer the rebuild after X ms of the last filesystem activity would be a lot useful.

[1] https://github.com/lucasew/hugo-kb

EDIT: I found a way to make it cooler: partialCached. Lots of partials were being rendered repeatedly but caching the partials helped a lot on hot reloads.

On Thu, Aug 5, 2021, 05:46 sjgknight @.***> wrote:

@gcushen https://github.com/gcushen I'm interested in this but not competent to implement. In looking around I've found two people discussing ways of doing this https://gabrielleearnshaw.medium.com/implementing-backlinks-in-a-hugo-website-e548d3d8f0e0 and https://seds.nl/notes/export_org_roam_backlinks_with_gohugo/ (I'm going to play, but I'm not likely to be able to implement). I can imagine academics e.g. being interested in linking projects<->publications<->authors<->news. Taxonomies/tags can do some of this I think, but it's not quite the same. @apreshill https://github.com/apreshill also has an "on this page" which shows the headings for a page, which I assume could be used to show the links https://github.com/rbind/apreshill/search?q=%22on+this+page%22&type= ? Apologies this is just a a link dump .

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/gohugoio/hugo/issues/8077#issuecomment-893281330, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADXXO6BSPLZRJF67VHSWFSDT3JFWRANCNFSM4VCP5IWQ .

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help. If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open. If this is a feature request, and you feel that it is still relevant and valuable, please tell us why. This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

BBaoVanC commented 1 year ago

I still think this is useful, especially for wikis which was mentioned above.

kaihendry commented 1 year ago

Zola has backlinks support, it would be good if Hugo has the same feature.

https://twitter.com/20100Prouillet/status/1548430360135929856

RenaudDev commented 1 year ago

It would be hugely useful for my project. Please make it happen.

carlklier commented 1 year ago

I want to bump this because I am looking for exactly this. Backlinks are incredibly useful

bryanwhiting commented 1 year ago

Bumping!

sacgov commented 1 year ago

Bumping!

Heinrichsgeist commented 1 year ago

Solution proposal (high level):

Performance should be good, but there's a lack of adequacy: Internal linkage changes will take effect with one build delay.

Heinrichsgeist commented 1 year ago

Solution proposal (high level):

* hugo build n collects the "truth about backlinks at n" and generates the "backlinks valid for build n-1"

* hugo build n+1 collects the "truth about backlinks at n+1" and generates the "backlinks valid for build n"

* hugo build n+2 collects the "truth about backlinks at n+2" and generates the "backlinks valid for build n+1"

* ...

Performance should be good, but there's a lack of adequacy: Internal linkage changes will take effect with one build delay.

This behaviour could be acceptable, e. g. when performance is no issue, or when there's a high frequency of site updates, or when the ratio of internal linkage changes is low.

leofujiike commented 1 year ago

How about Quartz? It is compatible with Hugo and comes with support for backlinks and a graph view, and it's easy to use. Repository: https://github.com/jackyzha0/quartz Setup Instructions: https://quartz.jzhao.xyz/notes/setup/

ManuelLevi commented 10 months ago

It would be great to have a native solution to this, instead of a third-party project like quartz.

bixfrankonis commented 9 months ago

This sort of feature is one of the things that has left me on WordPress, where me and ChatGPT designed a solution using post metadata. My blog basically has no other form of organizing content (no tags, etc.). Internal backlinks is a must for my use case, and I'd consider getting the hell off the ever more monstrous thing that is WordPress and onto a static approach if there were an internal backlinks solution.

jmooring commented 1 month ago

This uses a relatively simple module to capture and render backlinks.

git clone --single-branch -b hugo-github-issue-8077 https://github.com/jmooring/hugo-testing hugo-github-issue-8077
cd hugo-github-issue-8077
hugo server

Details: https://github.com/jmooring/hugo-module-backlinks

unindented commented 2 weeks ago

This uses a relatively simple module to capture and render backlinks.

git clone --single-branch -b hugo-github-issue-8077 https://github.com/jmooring/hugo-testing hugo-github-issue-8077
cd hugo-github-issue-8077
hugo server

Details: https://github.com/jmooring/hugo-module-backlinks

That's clever! Is there a way to make this work with {{< relref "..." >}} links?

jmooring commented 2 weeks ago

@unindented Use the {{% %}} notation when calling the relref shortcode.

[Post 1]({{% relref "post-1" %}})

However, the test site enables Hugo's embedded link render hook (see site configuration), so you shouldn't need to use the relref shortcode anywhere. The relref shortcode was essentially made obsolete when we introduced link render hooks a few years ago.