From bdd43736e4d18e387c1a6931107e2a3183d47fb1 Mon Sep 17 00:00:00 2001 From: Stewart MacKenzie-Leigh Date: Sun, 4 Aug 2013 17:48:06 +0100 Subject: [PATCH 1/2] Add $header_offset setting to allow headings to begin at say H2, for example if you have a page title then the markdown content after. --- Michelf/Markdown.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Michelf/Markdown.php b/Michelf/Markdown.php index 1dea9813..777279c0 100644 --- a/Michelf/Markdown.php +++ b/Michelf/Markdown.php @@ -58,6 +58,10 @@ public static function defaultTransform($text) { # Predefined urls and titles for reference links and images. public $predef_urls = array(); public $predef_titles = array(); + + # Offset for header levels, i.e., if you want the first heading to be H2, + # make the offset 1 + public $header_offset = 0; ### Parser Implementation ### @@ -769,12 +773,12 @@ protected function _doHeaders_callback_setext($matches) { if ($matches[2] == '-' && preg_match('{^-(?: |$)}', $matches[1])) return $matches[0]; - $level = $matches[2]{0} == '=' ? 1 : 2; + $level = $matches[2]{0} == '=' ? 1 : 2 + $this->header_offset; $block = "".$this->runSpanGamut($matches[1]).""; return "\n" . $this->hashBlock($block) . "\n\n"; } protected function _doHeaders_callback_atx($matches) { - $level = strlen($matches[1]); + $level = strlen($matches[1]) + $this->header_offset; $block = "".$this->runSpanGamut($matches[2]).""; return "\n" . $this->hashBlock($block) . "\n\n"; } From b765e5e20e5d804286beb6430140920739169e6f Mon Sep 17 00:00:00 2001 From: Stewart MacKenzie-Leigh Date: Sun, 4 Aug 2013 18:30:50 +0100 Subject: [PATCH 2/2] Adds header offset setting to MarkdownExtra as well. --- Michelf/Markdown.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Michelf/Markdown.php b/Michelf/Markdown.php index 777279c0..a5684a58 100644 --- a/Michelf/Markdown.php +++ b/Michelf/Markdown.php @@ -2475,13 +2475,13 @@ protected function doHeaders($text) { protected function _doHeaders_callback_setext($matches) { if ($matches[3] == '-' && preg_match('{^- }', $matches[1])) return $matches[0]; - $level = $matches[3]{0} == '=' ? 1 : 2; + $level = $matches[3]{0} == '=' ? 1 : 2 + $this->header_offset; $attr = $this->doExtraAttributes("h$level", $dummy =& $matches[2]); $block = "".$this->runSpanGamut($matches[1]).""; return "\n" . $this->hashBlock($block) . "\n\n"; } protected function _doHeaders_callback_atx($matches) { - $level = strlen($matches[1]); + $level = strlen($matches[1]) + $this->header_offset; $attr = $this->doExtraAttributes("h$level", $dummy =& $matches[3]); $block = "".$this->runSpanGamut($matches[2]).""; return "\n" . $this->hashBlock($block) . "\n\n";