Dynamic page linking
This is a really useful feature to have: allow users to link between pages in their rich text content. The links have to be dynamic, so that if the page is missing, this is resolved so that users don't end up with a 404 page.
How to do this
You only need three things:
- A QueryOpen agent to process the links in the content just before they are rendered.
- A view sorted on the page key.
- A LotusScript function to resolve the links.
In all of my web projects, the pages use an unique identifier: the page key. This is a value computed when the document is created by the @unique function. Furthermore, all my content is rendered by a QueryOpen agent and I already have a view sorted by this pagekey: my 'pub' view. So I only needed to define the syntax for the dynamic links and write the LotusScript agent to resolve them.
The syntax
This is an extension of the wiki-link syntax I've been using: [[@link:theKey|theLabel]]. In edit mode, this link is visible in it's original state. In read mode however, the link is resolved to a real hyperlink if the page exists or to a dummy link with a message on mouseover when the page is no longer there.
This is the LotusScript function to resolve the links:
Function resolveLinks(Byval sVal As String)As String
On Error Goto catch
Dim out As New Stringbuffer(200)
Dim arr As Variant, arr2 As Variant, arr3 As Variant
Dim i As Integer
Dim sLabel As String,sKey As String
Dim s As New NotesSession
Dim view As notesview
arr=Split(sVal, "[[")
'if no internal links present, then just return the string
If Ubound(arr)<1 Then
resolveLinks=sVal
Goto finally
End If
' resolve the internal links
Set view=s.CurrentDatabase.GetView("pub")
out.add arr(0)
For i=1 To Ubound(arr)
arr2=Split(arr(i), "]]")
arr3=Split(arr2(0), "|")
' get the label - if empty take a default one
If Instr(arr3(0), "@link:") Then
sKey=Mid$(arr3(0),7)
If Ubound(arr3)>0 Then
sLabel=arr3(1)
Else
sLabel="page: " & sKey
End If
If sLabel="" Then sLabel="page: " & sKey
' see if the page is there - if not, disable the link
If view.GetEntryByKey(sKey) Is Nothing Then
out.add |<a href="javascript:void null;" class="notFound" title="page not found">| & sLabel & |</a>|
Else
out.add |<a href="/domino/pub/| & sKey & |">| & sLabel & |</a>|
End If
Else
out.add |(!wrong link syntax: --| & arr2(0) & |--)|
End If
out.add arr2(1)
Next
resolveLinks=out.collapse("")
Goto finally
catch:
resolveLinks="Error" & Err & " in " & Getthreadinfo(1) & ", line " & Erl & ": " & Error$
Resume finally
finally:
End Function
Next
I don't expect users to fill in these tags manually. So I need an interface (a popup dialog) to insert these dynamic links. But that's another story...
Comments
To add a comment, log in or register as new user. It's free and safe.