From c7455012d4edb9da6bb334b9f4ef0bf47cbc2d98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 18 Aug 2018 20:30:19 +0200 Subject: [PATCH] Generate toc conditionnally --- docs/_plugins/tocmaker_block.rb | 18 ++++++++++++++---- docs/pages/release_notes.md | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/docs/_plugins/tocmaker_block.rb b/docs/_plugins/tocmaker_block.rb index 53b0eb28d9..5bcffe8095 100644 --- a/docs/_plugins/tocmaker_block.rb +++ b/docs/_plugins/tocmaker_block.rb @@ -1,14 +1,20 @@ # Generates a table of contents based on markdown headers in the body # -# The block's arg may be an int describing the maximum depth at which -# headers are added to the toc +# The block has 2 optional args: +# * A variable name. If provided, the toc will only be generated if the var is true +# * An integer, describing the maximum depth at which headers are added to the toc class TocMakerBlock < Liquid::Block # include Enumerable def initialize(tag_name, arg, tokens) super - @max_depth = arg.to_s.empty? ? 100 : arg.to_i + + condition, depth = arg.split + + @max_depth = depth.to_s.empty? ? 100 : depth.to_i + @condition_var = condition.strip + @body = tokens end @@ -22,6 +28,10 @@ class TocMakerBlock < Liquid::Block contents = @body.render(context) + if @condition_var && !context[@condition_var] + # If the condition is false, the toc is not generated + return contents + end headers = contents.lines.map {|l| if /^(#+)\s+(\S.*)$/ =~ l @@ -29,7 +39,7 @@ class TocMakerBlock < Liquid::Block end }.compact - min_indent = headers.min_by {|t| t[0]}[0] + min_indent = headers.map {|t| t[0]}.min headers = headers.map {|t| actual_depth = t[0] - min_indent diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index efa8e49230..4756d84652 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -10,7 +10,7 @@ The PMD team is pleased to announce PMD {{ site.pmd.version }}. This is a {{ site.pmd.release_type }} release. -{% tocmaker %} +{% tocmaker is_release_notes_processor %} ### New and noteworthy