[FR] - Manipulate Heading Levels
animaux opened this issue · 2 comments
Moving to Craft recently I have still to find a failsafe way for the following usecase:
Depending on context it should be possible to raise the level of all headings in a certain text field (markdown —> html, or rich text editor with html-output).
- For example, if the site uses
h1
as general logo/site-header, anyh1
s in a text-field should becomeh2
, an increase by+1
. - Or, if a page/article title comes from a dedicated field and the template sets it in a
<h2>
, theh1
in the actual text field should be increased by+2
, so it becomesh3
. - Third example is usage of a text field in a blog entry hierarchy. F. e.
h1
is site logo,h2
is »Blog«, so the entry title has to beh3
and the first usable heading level in the actual blog entry should beh4
, etc.
The main idea behind this is to save the user/author from having to care about outer heading levels. This way an author only has to take care about the inner hierarchy in a text field without having to reflect it’s context.
Usage could look like this:
{{ entry.text|hlevel('+1') }}
{{ entry.text|hlevel('+2') }}
Maybe a negative manipulation could also make sense in some context:
{{ entry.text|hlevel('-1') }}
@animaux I'd typically handle this with Twig's replace filter, though I'd normally alter only the appearance of the element in user-entered (Redactor) content, you could change the heading element instead with the same method.
{{ entry.richText|replace({
'<h2>': '<h2 class="h3">',
'<h3>': '<h3 class="h4">',
'<h4>': '<h4 class="h5">'
})|typogrify }}
And I always customise Redactor so it's not possible to use an h1 in the first place.
Agree with @nternetinspired