Add open code block with any number of chars

pull/1/head
Leonid Maslakov 5 months ago
parent 14658947b5
commit d1cb53e243
  1. 2
      docs/syntax_guide.md
  2. 14
      md2html.go
  3. 43
      md2html_code_block.go
  4. 31
      md2html_test.go

@ -57,7 +57,7 @@ go help build
`````markdown
````markdown
```golang
```bash
go doc
go tool dist list
go help build

@ -63,6 +63,14 @@ const lineSeparator = "\n"
// go help build
// ```
//
// ````markdown
// ```
// go doc
// go tool dist list
// go help build
// ```
// ````
//
// Unordered list ('-' may be replaced by '+' or '*'):
// - level 1
// - level 1
@ -146,11 +154,7 @@ func Convert(text string) string {
// If code block: <pre><code>
} else if strings.HasPrefix(line, "```") {
if strings.HasPrefix(line, "````") {
codeTagCloseLine = "````"
} else {
codeTagCloseLine = "```"
}
codeTagCloseLine, _ = mdCodeBlock(line)
if pTagInBuffer == true {
line = "<p>" + baseMdFormat(buffer) + "</p><pre><code>"

@ -0,0 +1,43 @@
// Copyright (C) 2022 Leonid Maslakov.
// This file is part of md2html.
// md2html is free software: you can redistribute it
// and/or modify it under the terms of the
// GNU Affero Public License as published by the
// Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
// md2html is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero Public License for more details.
// You should have received a copy of the GNU Affero Public License along with md2html.
// If not, see <https://www.gnu.org/licenses/>.
package md2html
// Parse the line that opens the code block.
// Return open line (e.g ``` or `````) and language name (e.g. "" or "python").
//
// WARNING: You must be sure that the line is correct.
func mdCodeBlock(line string) (string, string) {
openLine := ""
codeLang := ""
lineRune := []rune(line)
for i := range lineRune {
charRune := lineRune[i]
if charRune == '`' {
openLine = openLine + "`"
} else {
codeLang = codeLang + string(charRune)
}
}
return openLine, codeLang
}

@ -355,6 +355,37 @@ func TestConvert(t *testing.T) {
Input: "``",
ExpectResult: "<p>``</p>",
},
// Code
{
Input: "`my code`",
ExpectResult: "<p><code>my code</code></p>",
},
{
Input: "```python\n" +
"go doc\n" +
"go tool dist list\n" +
"go help build\n" +
"```\n",
ExpectResult: `<pre><code>go doc
go tool dist list
go help build
</code></pre>`,
},
{
Input: "````markdown\n" +
"```\n" +
"go doc\n" +
"go tool dist list\n" +
"go help build\n" +
"```\n" +
"````\n",
ExpectResult: "<pre><code>```\n" +
"go doc\n" +
"go tool dist list\n" +
"go help build\n" +
"```\n" +
"</code></pre>",
},
// Unordered list
{
Input: `

Loading…
Cancel
Save