[aur-dev] [PATCH v2 0/3] Add support for markdown code blocks and inline code
Only a subset of markdown is support, i.e.: ``` code blocks ``` and `inline code`. Marcel Korpel (3): Add horizontal scrollbar to code blocks when necessary Use a normal font size for code blocks Convert markdown codeblocks and inline code to HTML web/html/css/aurweb.css | 5 ++++ web/lib/aur.inc.php | 74 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 65 insertions(+), 14 deletions(-) -- 2.4.5
Signed-off-by: Marcel Korpel <marcel.korpel@gmail.com> --- web/html/css/aurweb.css | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/web/html/css/aurweb.css b/web/html/css/aurweb.css index b33726c..6bd7eb3 100644 --- a/web/html/css/aurweb.css +++ b/web/html/css/aurweb.css @@ -124,6 +124,10 @@ opacity: 1; } +pre { + overflow-x: auto; +} + legend { padding: 1em 0; } -- 2.4.5
Signed-off-by: Marcel Korpel <marcel.korpel@gmail.com> --- web/html/css/aurweb.css | 1 + 1 file changed, 1 insertion(+) diff --git a/web/html/css/aurweb.css b/web/html/css/aurweb.css index 6bd7eb3..691a634 100644 --- a/web/html/css/aurweb.css +++ b/web/html/css/aurweb.css @@ -125,6 +125,7 @@ } pre { + font-size: 100%; overflow-x: auto; } -- 2.4.5
Only codeblocks delimited with ```<newline> are supported. Signed-off-by: Marcel Korpel <marcel.korpel@gmail.com> --- Changes from v1: * Convert one line at a time * Use preg_replace_callback to convert inline code and URLs * Now URLs within inline code are also converted, unwanted behaviour? * Introduces a global ($md_inline_code_active), unwanted? web/lib/aur.inc.php | 74 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 60 insertions(+), 14 deletions(-) diff --git a/web/lib/aur.inc.php b/web/lib/aur.inc.php index 411d5ee..d370788 100644 --- a/web/lib/aur.inc.php +++ b/web/lib/aur.inc.php @@ -603,25 +603,71 @@ function comment_by_id($comment_id) { * @return string User comment with links printed in HTML */ function parse_comment($comment) { - $url_pattern = '/(\b(?:https?|ftp):\/\/[\w\/\#~:.?+=&%@!\-;,]+?' . - '(?=[.:?\-;,]*(?:[^\w\/\#~:.?+=&%@!\-;,]|$)))/iS'; - - $matches = preg_split($url_pattern, $comment, -1, - PREG_SPLIT_DELIM_CAPTURE); + $url_pattern = '/\b(?:https?|ftp):\/\/[\w\/\#~:.?+=&%@!\-;,]+?' . + '(?=[.:?\-;,]*(?:[^\w\/\#~:.?+=&%@!\-;,]|$))/iS'; + $md_codeblock_delimiter = '```'; + $md_codeblock_active = false; + $md_inline_code_delimiter = '/`/'; + $md_inline_code_active = false; $html = ''; - for ($i = 0; $i < count($matches); $i++) { - if ($i % 2) { - # convert links - $html .= '<a href="' . htmlspecialchars($matches[$i]) . - '">' . htmlspecialchars($matches[$i]) . '</a>'; - } - else { - # convert everything else - $html .= nl2br(htmlspecialchars($matches[$i])); + $lines = preg_split('/\R/', $comment); + + foreach ($lines as $line) { + if ($line == $md_codeblock_delimiter) { + if ($md_codeblock_active) { + $html .= '</code></pre><p>'; + $md_codeblock_active = false; + } else { + $html .= '</p><pre><code>'; + $md_codeblock_active = true; + } + } else { + if ($md_codeblock_active) { + $html .= htmlspecialchars($line) . "\n"; + } else { + global $md_inline_code_active; + + $line = htmlspecialchars($line); + $line = preg_replace_callback( + $md_inline_code_delimiter, + function ($matches) { + global $md_inline_code_active; + + if ($md_inline_code_active) { + $md_inline_code_active = false; + return '</code>'; + } else { + $md_inline_code_active = true; + return '<code>'; + } + }, + $line); + + $line = preg_replace_callback( + $url_pattern, + function ($matches) { + return '<a href="' . $matches[0] . + '">' . $matches[0] . '</a>'; + }, + $line); + + $html .= $line . "<br />\n"; + } } } + // close possible open inline code + if ($md_inline_code_active) { + $html .= '</code>'; + $md_inline_code_active = false; + } + + // close possible open code block + if ($md_codeblock_active) { + $html .= '</code></pre><p>'; + } + return $html; } -- 2.4.5
Signed-off-by: Marcel Korpel <marcel.korpel@gmail.com> --- web/lib/aur.inc.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/web/lib/aur.inc.php b/web/lib/aur.inc.php index d370788..7e85959 100644 --- a/web/lib/aur.inc.php +++ b/web/lib/aur.inc.php @@ -596,7 +596,9 @@ function comment_by_id($comment_id) { } /** - * Process submitted comments so any links can be followed + * Process submitted comments so any links can be followed and + * parse markdown code blocks and inline code. Only code blocks + * delimited with ```<newline> are supported. * * @param string $comment Raw user submitted package comment * -- 2.4.5
If the comment ends with a newline, a superfluous <br> is inserted, resulting in extra whitespace at the end of the comment. Signed-off-by: Marcel Korpel <marcel.korpel@gmail.com> --- web/lib/aur.inc.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/web/lib/aur.inc.php b/web/lib/aur.inc.php index 7e85959..6c0532c 100644 --- a/web/lib/aur.inc.php +++ b/web/lib/aur.inc.php @@ -614,8 +614,9 @@ function parse_comment($comment) { $html = ''; $lines = preg_split('/\R/', $comment); + $len = count($lines); - foreach ($lines as $line) { + foreach ($lines as $key => $line) { if ($line == $md_codeblock_delimiter) { if ($md_codeblock_active) { $html .= '</code></pre><p>'; @@ -654,7 +655,10 @@ function parse_comment($comment) { }, $line); - $html .= $line . "<br />\n"; + $html .= $line; + if ($key < $len - 1) { + $html .= "<br />\n"; + } } } } -- 2.4.5
participants (1)
-
Marcel Korpel