diff --git a/cmd/tools/vdoc/html.v b/cmd/tools/vdoc/html.v index eec8a5181e03d7..05db9bc9abe660 100644 --- a/cmd/tools/vdoc/html.v +++ b/cmd/tools/vdoc/html.v @@ -475,7 +475,13 @@ fn html_highlight(code string, tb &ast.Table) string { } buf.write_string('') - write_token(tok, tok_typ, mut buf) + if tok_typ == .string { + // Make sure to escape html in strings. Otherwise it will rendered in the + // html documentation outputs / its style rules will affect the readme. + buf.write_string("'${html.escape(tok.lit.str())}'") + } else { + write_token(tok, tok_typ, mut buf) + } buf.write_string('') } @@ -627,15 +633,10 @@ fn (f &MdHtmlCodeHighlighter) transform_attribute(p markdown.ParentType, name st } fn (f &MdHtmlCodeHighlighter) transform_content(parent markdown.ParentType, text string) string { - // NOTE: markdown.default_html_transformer uses html.escape internally. - initial_transformed_text := markdown.default_html_transformer.transform_content(parent, - text) if parent is markdown.MD_BLOCKTYPE && parent == .md_block_code { - if f.language == 'v' || f.language == 'vlang' { - return html_highlight(html.unescape(initial_transformed_text), f.table) - } + return html_highlight(text, f.table) } - return initial_transformed_text + return text } fn (mut f MdHtmlCodeHighlighter) config_set(key string, val string) { diff --git a/cmd/tools/vdoc/tests/testdata/output_formats/README.md b/cmd/tools/vdoc/tests/testdata/output_formats/README.md index 3226fb1676c7df..ba5f6a861e8017 100644 --- a/cmd/tools/vdoc/tests/testdata/output_formats/README.md +++ b/cmd/tools/vdoc/tests/testdata/output_formats/README.md @@ -79,3 +79,54 @@ fn auth_verify(secret string, token string) bool { return hmac.equal(signature_from_token, signature_mirror) } ``` + +### Other language specifiers + +```cpp +#include +#include + +std::map my_map { + {"KEY_1", 0}, + {"KEY_2", 10}, +}; + +for (const auto &[key, value] : my_map) { + std::cout << key << ": " << value << ", "; +} +std::cout << "\n"; +``` + +```v ignore +doc1 := toml.parse_text() or { panic(err) } +doc2 := toml.parse_file() or { panic(err) } +``` + +### Escape html in strings + +```v +const html = ' + + + + + +

Your App Content!

+ + + +' +``` diff --git a/cmd/tools/vdoc/tests/testdata/output_formats/main.ansi b/cmd/tools/vdoc/tests/testdata/output_formats/main.ansi index c99e4a6b72522c..66a1d61bbbfe39 100644 --- a/cmd/tools/vdoc/tests/testdata/output_formats/main.ansi +++ b/cmd/tools/vdoc/tests/testdata/output_formats/main.ansi @@ -67,6 +67,49 @@ fn auth_verify(secret string, token string) bool { signature_from_token := base64.url_decode(token_split[2]) return hmac.equal(signature_from_token, signature_mirror) } + +Other language specifiers +#include +#include + +std::map my_map { + {"KEY_1", 0}, + {"KEY_2", 10}, +}; + +for (const auto &[key, value] : my_map) { + std::cout << key << ": " << value << ", "; +} +std::cout << "\n"; + +doc1 := toml.parse_text() or { panic(err) } +doc2 := toml.parse_file() or { panic(err) } + +Escape html in strings +const html = ' + + + + + +

Your App Content!

+ + + +' module main ## Description @@ -149,6 +192,57 @@ fn auth_verify(secret string, token string) bool { } ``` + ### Other language specifiers + + ```cpp + #include + #include + + std::map my_map { + {"KEY_1", 0}, + {"KEY_2", 10}, + }; + + for (const auto &[key, value] : my_map) { + std::cout << key << ": " << value << ", "; + } + std::cout << "\n"; + ``` + + ```v ignore + doc1 := toml.parse_text() or { panic(err) } + doc2 := toml.parse_file() or { panic(err) } + ``` + + ### Escape html in strings + + ```v + const html = ' + + + + + +

Your App Content!

+ + + + ' + ``` + const omega = 3 // should be first const alpha = 5 // should be in the middle diff --git a/cmd/tools/vdoc/tests/testdata/output_formats/main.html b/cmd/tools/vdoc/tests/testdata/output_formats/main.html index 3dcedb9e71af03..38813c787936db 100644 --- a/cmd/tools/vdoc/tests/testdata/output_formats/main.html +++ b/cmd/tools/vdoc/tests/testdata/output_formats/main.html @@ -57,7 +57,40 @@

Description

This is an example of a an .md file, used for adding more sha256.sum, sha256.block_size) signature_from_token := base64.url_decode(token_split[2]) return hmac.equal(signature_from_token, signature_mirror) -} +}

Other language specifiers

##
+std::map<std::string, int> my_map {
+    {'KEY_1', 0},
+    {'KEY_2', 10},
+};
+
+for (const auto &[key, value] : my_map) {
+    std::cout << key << ': ' << value << ', ';
+}
+std::cout << '\n';
doc1 := toml.parse_text(<string content>) or { panic(err) }
+doc2 := toml.parse_file(<file path>) or { panic(err) }

Escape html in strings

const html = '<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <style>
+      body {
+        background: linear-gradient(to right, #274060, #1B2845);
+        color: GhostWhite;
+        font-family: sans-serif;
+        text-align: center;
+      }
+    </style>
+  </head>
+  <body>
+    <h1>Your App Content!</h1>
+    <button onclick="callV()">Call V!</button>
+  </body>
+  <script>
+    async function callV() {
+      // Call a V function that takes an argument and returns a value.
+      const res = await window.my_v_func(\'Hello from JS!\');
+      console.log(res);
+    }
+  </script>
+</html>'
diff --git a/cmd/tools/vdoc/tests/testdata/output_formats/main.text b/cmd/tools/vdoc/tests/testdata/output_formats/main.text index d36f21bfba4fb9..092472890685cf 100644 --- a/cmd/tools/vdoc/tests/testdata/output_formats/main.text +++ b/cmd/tools/vdoc/tests/testdata/output_formats/main.text @@ -80,6 +80,57 @@ module main } ``` + ### Other language specifiers + + ```cpp + #include + #include + + std::map my_map { + {"KEY_1", 0}, + {"KEY_2", 10}, + }; + + for (const auto &[key, value] : my_map) { + std::cout << key << ": " << value << ", "; + } + std::cout << "\n"; + ``` + + ```v ignore + doc1 := toml.parse_text() or { panic(err) } + doc2 := toml.parse_file() or { panic(err) } + ``` + + ### Escape html in strings + + ```v + const html = ' + + + + + +

Your App Content!

+ + + + ' + ``` + const omega = 3 // should be first const alpha = 5 // should be in the middle