Skip to content

Commit

Permalink
Fix Markdown handling of # characters
Browse files Browse the repository at this point in the history
- remove incorrect logic to identify headers
- ensure header detection abides by the CommonMark spec rules [1]
- remove isBugLink method, which is no longer needed since bug links
  (i.e. `#number`) are not recognized as a header by the new regex

Fixes #22333

[1] http://spec.commonmark.org/0.27/#atx-headings

Signed-off-by: Damien Regad <dregad@mantisbt.org>

Changes to Joel's original code: squashed 2 commits, reworded commit
message as well as the comments within the code.
  • Loading branch information
jllano authored and dregad committed Mar 31, 2017
1 parent ca40d68 commit e4c0cce
Showing 1 changed file with 9 additions and 51 deletions.
60 changes: 9 additions & 51 deletions plugins/MantisCoreFormatting/core/MantisMarkdown.php
Expand Up @@ -97,52 +97,21 @@ public static function convert_line( $p_text ) {
}

/**
* Customize the logic on Header elements
* Customize the blockHeader markdown
*
* @param string $line The Markdown syntax to parse
* @access protected
* @return string|null HTML representation generated from markdown or
* null if markdown starts with # symbol
* @return string|null HTML representation generated from markdown or null
* if text is not a valid header per CommonMark spec
*/
protected function blockHeader( $line ) {
$block = parent::blockHeader( $line );

# Bug links should not be treated as headers
if( $this->isBugLink( $line['text'] ) ) {
return null;
}

# Header rules
# hash[space][numbers] - treated as header
# hash[number][*] - treated as header since it is not a pure number
# hash[letter][*] - treated as header
# hash[space][letter][*] - treated as header
return $block;
}

/**
* Customize the logic on setting the Header elements.
*
* @param string $line The Markdown syntax to parse
* @param array $block A block-level element
* @access protected
* @return string|null HTML representation generated from markdown or
* null if markdown starts with # symbol
*/
protected function blockSetextHeader( $line, array $block = null ) {
$block = parent::blockSetextHeader( $line, $block );

# Bug links should not be treated as headers
if( $this->isBugLink( $line['text'] ) ) {
return null;
# Header detection logic
# - the opening # may be indented 0-3 spaces
# - a sequence of 1–6 '#' characters
# - The #'s must be followed by a space or a newline
if ( preg_match( '/^ {0,3}#{1,6}(?: |$)/', $line['text'] ) ) {
return parent::blockHeader($line);
}

# Header rules
# hash[space][numbers] - treated as header
# hash[number][*] - treated as header since it is not a pure number
# hash[letter][*] - treated as header
# hash[space][letter][*] - treated as header
return $block;
}

/**
Expand Down Expand Up @@ -325,15 +294,4 @@ private function processAmpersand( &$p_text ) {
$p_text = str_replace( '&amp;', '&', $p_text );
}

/**
* Check if the given string is a bug link reference.
*
* @param string $p_text
* @return bool
*/
private function isBugLink( $p_text ) {
return '#' == config_get_global( 'bug_link_tag' )
&& preg_match_all( '/^#\d+$/', $p_text, $matches );
}

}

0 comments on commit e4c0cce

Please sign in to comment.