Linking to a specific line (or header) of a section

When you are editing your document, coda actually remembers the line your cursor is in. This is the part after the # in the url.
What’s more, each line has its unique id which doesn’t change even if you add new lines above or below.
(Just remember that if you are adding new line like this (the cursors is at the beginning of the line)

you are actually moving all text in that line to a new one, which will have a different id.)

That means that just by using the url with the # part, you can link to that specific line.

Things to note
Unfortunately, this method has some quirks.
The most notable one is that the url actually moves the cursor to this position, not the user.
That means that the document will scroll the minimal amount for that line to be visible. So if you link to a line below currently visible area, the document will move so that that line is now the bottom one. This also means that if a line is already visible, the document won’t scroll at all.

There is another big issue with this method - it won’t work if the cursor is already on the line, even if the line itself isn’t visible (so if you were to click the link, then scroll back down and click it again, it won’t work the second time until you move your cursor to a different line).

There are two ways to get rid of this issue.
You could place all your links in a different section (this would work ok for indexes).
You can also use a button which will firstly move your cursor to the current line and after that to the target one. Even if your cursor is on the current line, the second link will work and move you to the correct line.
In order for the links to be launched one after another, instead of two at once, you need to add _Delay("",0) in between them.

The result formula should look something like this:

RunActions(
OpenWindow(Url(thisDocument)+"/_sut0w#_lu3ke"),
_Delay("",0),
OpenWindow(Url(thisDocument)+"/_sut0w#_lu_ak")
)

Where the first url is the one to the current line, and the second one is to the target line.
This unfortunately adds a small delay before the link works.

Pseudo-headers
If you’d want your link to always move the target line to the top of the document, you could create a view of a table for each of your headers. The table name would be the header and its first row would be the content (there is a document below with an example implementation).
Unfortunately, this doesn’t work very well on mobile.

There is stil the issue of the link not working twice, but you can fix this in the exactly the same way as line links.

Sandbox

7 Likes

jeez! this was SUPER helpful. Thanks so much.