Orgmode how to name headlines anchors

when i do this in orgmode:

* Who Should Support Us
** Law Enforcement
** Environmental Protection

i get this in html:

<li><a href="#headline-1">Who Should Support Us</a>
<li><a href="#headline-2">Law Enforcement</a>
<li><a href="#headline-3">Environmental Preservation</a>

which is fine unless i want to add headings in between, because then the anchor ref changes. any links i’ve already setup within the website or given out, go to an incorrect place.

is there a way to make the headline ref absolute in the sense that
#headline-1 would be #Who-Should-Support-Us
so the order won’t change the links themselves?

i can’t figure out how to do it from
Configure Markup

and though i could do something like this (kaushalmodi code):

{{ . | replaceRE "(<h[2-9] id=\"([^\"]+)\".+)(</h[2-9]+>)" "${1}&nbsp;<a class=\"headline-hash\" href=\"#${2}\">#</a> ${3}" | safeHTML }}

or write my own replacement script to act on the html code externally,

it would be nice if there were already some mechanism that is available to deal with this.

If you are using Hugo’s native Org parsing, then that id generation is done by the goorg package: GitHub - niklasfasching/go-org: Org mode parser with html & pretty printed org rendering. also shitty static site generator..

You can open an Issue on that repo to check with the author if what you want is supported.

thx for this kaushalmodi!

i have been using whatever comes with hugo, because i read somewhere that it is what we should use (can’t remember where, but something to do with blackfriday markdown).

however, this is the second time ox-hugo has been mentioned.

i would appreciate knowing what i really should use (if such a statement can be made) and possibly why.

i did find this discussion:
Ox-hugo & go-org

and am tempted to try ox-hugo.

and this:
Why ox-hugo?

ya i’m really tempted!

i would appreciate knowing what i really should use

Yeah, I wouldn’t make that statement for anyone. You need to try out all the approaches and pick what works the best for you.

I happen to like the ox-hugo approach :wink:

ok k!
i’ve been reading up on ox-hugo and will try it tonight.

there is an item saying conversion from markdown isn’t necessary, which is understandable, but my site has been done in org-mode (then go-orged), so i presume most of it should work with no problem. however, the front-matter (from what i see on the ox-hugo site) is more extensive and a bit different.

for instance, i just have something like this:

type or paste code here---
title: Media
date: 2023-01-02T22:20:11-08:00
author: prauthor
description: Media articles and other items published regarding fireworks issues.
categories: [wellnes,erudite,operate]
tags: [fireworks,media]

which is quite different from the screenshots here.
not a big deal of course, but just wondering if there is a front-matter ‘converter’?

i can’t get ox-hugo to load.

i’ve followed the steps in the quickstart.

i’ve tried ox-hugo from both melpa and melpa-stable. one error i was getting in messages with the former was there was no ox-hugo-autoloads

right now i’m trying ox-hugo-0.12.2 from melpa-stable, but the C-c C-e doesn’t give me H H. however, i’m not getting the autoloads error.

i’ve have as stated in the quickstart

(with-eval-after-load 'ox
  (require 'ox-hugo))

the only way i get anywhere is to
M-x load-file
that at leasts gives me C-c C-e H H
and things seem to work, though, sometimes i get
Cannot use unknown “blackfriday” back-end as a parent
(if i DO have the require 'ox-hugo code)

so why is the (require 'ox-hugo) bit not working?

though i’d still like to get ox-hugo working, i’ve solved my issue with absolutizing the #headlines-x with the following script:

#!/usr/bin/env zsh

# absolutifies headlines for hugo generated html

# mechanism:
# take all #headline-x
# replace it with the actual headline wording (spaces -> -)

# example:
# <li><a href="#headline-1">Structure of Site</a>
# becomes
# <li><a href="#Structure-of-Site">Structure of Site</a>

# for the file

# grab all the #headline-x and its description then split into array on \n
str=$(perl -ne 'print "$1|$2\n" if /<li><a href="(#headline-\d+)">(.+)<\/a>/' $F)

# using assoc array (only because i've never tried it before)
declare -A aa
for l in $lines; do
    # split into assoc array
    key=${${(k)aa}#\#} #rm the '#' symbol
    lnk=${val// /-} #replace sp with -

    #echo "$key -> $val -> $lnk"

    # do the replacements
    perl -pi -e "s/$key/$lnk/g" $F


the files i go after (just because of the way my site is setup) are the index.html in updated public/ dir after we hugo the content/ dir.

i use find to pick out the index.html files in the specific directory i’m interested in and feed these to the above script,

find $DIR -name "index.html" -exec "{}" \;

so the ‘Human Safety’ link in the toc of this page:
was changed from being the sometimes inconvenient

and if it changes position later on to say #headline-9, i won’t have to rewrite all the links pointing to it.

imho, it would be nice if this sort of absolutization of headings could be added to hugo, but this script does the job for now.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.