Item14905: Use H1 header as TopicTitle

pencil
Priority: Enhancement
Current State: New
Released In: n/a
Target Release:
Applies To: Extension
Component: TopicTitlePlugin
Branches:
Reported By: ColasNahaboo
Waiting For: ColasNahaboo
Last Change By: ColasNahaboo
My suggestion: use the first H1 header, be it in HTML (<h1>) or TML (---+) as the Topic Title.

For most web pages, the header H1 in its contents is semantically its natural "title", so I was looking for a way to implement it. I found the TopicTitlePlugin, but it used more complex mecanisms (preferences, form) than just the H1. And by digging in old dicsussions in TopicDisplayName, I saw that what I was looking for was exactly was what Tobias Roeser descibed initially. But this idea was abandoned, out of performance issues. But now we have the great and efficient implementation of the TopicTitlePlugin, these performance issues are solved, so why not re-introduce the initial idea?

I thus made a simple addition to TopicTitlePlugin that on saving a page sets the TopicTitle to the contents of the first <h1> or ---+ or ---+!! on the page if none is set by a form. It thus uses all the performance enhancements of the plugin (caching and saving as a topic preference), with nearly no overhead (as it is only on save, and the topic text is already in memory, and we search it only until we hit the H1, which should be quite close to top).

It will however take precedence over a topic preference of TopicTitle, as otherwise, as it is saved as a preference, changing the H1 topic would not have worked. For not breaking backwards compatibility, I thus made using the H1 header optional, with a preference setting. So to enable the feature, define:
   * Set TOPICTITLE_H1 = on

I have been using this attached patch for some days and it seems to be OK, but as always, it may have unexpected consequences. I will run it on my sites for some time for more testing. Apply with patch -p0 in the root Foswiki dir, Patch made on the v1.02 of the plugin.

(Version 2, 2020-05-10)
--- lib/Foswiki/Plugins/TopicTitlePlugin/Core.pm        2020-05-05 09:15:53.118078055 +0200
+++ lib/Foswiki/Plugins/TopicTitlePlugin/Core.pm.new    2020-05-05 09:15:53.078077730 +0200
@@ -134,7 +134,21 @@
     my $topicTitleField =
       $meta->getPreference("TOPICTITLE_FIELD") || "TopicTitle";
 
-    my $topicTitle = $request->param($topicTitleField);
+    # Patch_TopicTitleH1,begin
+    # Optionally, use the first H1 as TOPICTITLE
+    my $topicTitle;
+    if ( Foswiki::Func::getPreferencesValue("TOPICTITLE_H1") &&
+       $text =~ m/(^---[+]!*\s*([^+].*)$|<h1>(.+)(?=<\/h1))/mi ) {
+       $topicTitle = defined($2) ? $2 : $3;
+       $topicTitle =~ s/\s+$//;
+       $topicTitle = $meta->expandMacros($topicTitle);
+       _writeDebug("TopicTitle set to H1: $topicTitle") if TRACE;
+    }
+    unless ( defined $topicTitle && $topicTitle ne '' ) {
+       $topicTitle = $request->param($topicTitleField);
+    }
+    # Patch_TopicTitleH1,end
+
     unless ( defined $topicTitle ) {
         _writeDebug("didn't get a TopicTitle, nothing do here") if TRACE;
         return;

Caveat: enabling this feature will do nothing for existing topics (their title will not use their H1), until they are edited and saved.

Variant: I also toyed with the idea of just setting a %TOPICTITLE% or %TITLE% during the rendering pipeline of headers. It would have solved the issue of existing topics, with nearly no overhead, but would have been a separate feature from TopicTitle, simpler but with less flexibility, and less power as it does not provide the functionality to get the Title of other topics.

-- ColasNahaboo - 07 May 2020

Version 1 of the patch had a bug, where it could use the first Hn (H2, H3,...) in the topic if no H1 were found. I uploaded the version 2 as attachment, and correct the verbatim inline version above. The only change is in the regex in the $text =~... line

-- ColasNahaboo - 10 May 2020
 
Topic attachments
I Attachment Action Size Date Who Comment
Colas_TopicTitleH1.patchpatch Colas_TopicTitleH1.patch manage 1 K 10 May 2020 - 07:41 ColasNahaboo Patch against TopicTitlePlugin v1.02. Version 2
Topic revision: r2 - 10 May 2020, ColasNahaboo - This page was cached on 07 Jun 2020 - 06:13.

The copyright of the content on this website is held by the contributing authors, except where stated elsewhere. See Copyright Statement. Creative Commons License    Legal Imprint    Privacy Policy