Add support for rebasing headings at render time

Fixes #3330
single-tiddler-mode
Jermolene 2018-06-10 15:54:23 +01:00
rodzic 29606c6d24
commit 2e51f08bef
6 zmienionych plików z 85 dodań i 0 usunięć

Wyświetl plik

@ -35,6 +35,14 @@ ElementWidget.prototype.render = function(parent,nextSibling) {
if($tw.config.htmlUnsafeElements.indexOf(tag) !== -1) {
tag = "safe-" + tag;
}
// Adjust headings by the current base level
var headingLevel = ["h1","h2","h3","h4","h5","h6"].indexOf(tag);
if(headingLevel !== -1) {
var baseLevel = parseInt(this.getVariable("tv-adjust-heading-level","0"),10) || 0;
headingLevel = Math.min(Math.max(headingLevel + 1 + baseLevel,1),6);
tag = "h" + headingLevel;
}
// Create the DOM node
var domNode = this.document.createElementNS(this.namespace,tag);
this.assignAttributes(domNode,{excludeEventAttributes: true});
parent.insertBefore(domNode,nextSibling);

Wyświetl plik

@ -0,0 +1,22 @@
created: 20180609112450207
modified: 20180609112450207
title: Sample Headings 1,2,3
! This is a Level 1 Heading
This is a paragraph.
<$vars tv-adjust-heading-level="-2">
{{Sample Headings 3,4,5}}
</$vars>
!! This is a Level 2 heading
<$vars tv-adjust-heading-level="-1">
{{Sample Headings 4,5,6}}
</$vars>

Wyświetl plik

@ -0,0 +1,9 @@
created: 20180609112450207
modified: 20180609112450207
title: Sample Headings 3,4,5
!!! This is written as a Level 3 heading
!!!! This is written as a Level 4 heading
!!!!! This is written as a Level 5 heading

Wyświetl plik

@ -0,0 +1,9 @@
created: 20180609112450207
modified: 20180609112450207
title: Sample Headings 4,5,6
!!!! This is written as a Level 4 heading
!!!!! This is written as a Level 5 heading
!!!!!! This is written as a Level 6 heading

Wyświetl plik

@ -0,0 +1,25 @@
created: 20180609112450207
modified: 20180609112450207
tags: [[tv-adjust-heading-level Variable]] [[Variable Examples]]
title: tv-adjust-heading-level Variable (Examples)
type: text/vnd.tiddlywiki
\define show-tiddler(title)
!! $title$
<$codeblock code={{$title$}}/>
\end
! Tiddlers
<<show-tiddler "Sample Headings 1,2,3">>
<<show-tiddler "Sample Headings 3,4,5">>
<<show-tiddler "Sample Headings 4,5,6">>
! Example
<$macrocall $name=".example" n="1"
eg="""<$transclude tiddler="Sample Headings 1,2,3" mode="block"/>"""/>

Wyświetl plik

@ -0,0 +1,12 @@
caption: tv-adjust-heading-level
created: 20180609112450207
modified: 20180609112450207
tags: Variables [[Core Variables]] [[Configuration Variables]]
title: tv-adjust-heading-level Variable
type: text/vnd.tiddlywiki
The <<.def tv-adjust-heading-level>> [[variable|Variables]] allows the level of [[headings|Headings in WikiText]] to be adjusted. It is interpreted as a positive or negative number (e.g. "2" or "-3") that is added to the heading level for display.
Note that if the resulting heading level is less than 1 then level 1 is used instead. This means that specfiying a large negative adjustment factor will reset all headings to display as level 1. Similarly, heading levels larger than 6 are clamped to the maximum HTML heading level of 6.
<<.variable-examples "tv-adjust-heading-level">>