Skip to content

Commit

Permalink
feat(gnoweb): prettify json code blocks in render (#2033)
Browse files Browse the repository at this point in the history
<!-- please provide a detailed description of the changes made in this
pull request. -->
## Prettify JSON code blocks in gnoweb render

- add hljs json language support
- upgrade marked
- add marked-highlight to plug marked with hljs
- format json before highlighting
- fix horizontal overflow in code blocks

## Before
<img width="1511" alt="Screenshot 2024-05-03 at 19 42 59"
src="https://github.com/gnolang/gno/assets/7917064/9d226d33-8caf-4f23-86d1-59b3fbd84d7b">

## After
<img width="1512" alt="Screenshot 2024-05-03 at 19 41 21"
src="https://github.com/gnolang/gno/assets/7917064/535e5bad-f624-452c-83bd-9430db08392a">


<details><summary>Contributors' checklist...</summary>
- [ ] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>

Signed-off-by: Norman Meier <[email protected]>
  • Loading branch information
n0izn0iz authored May 7, 2024
1 parent 143fa9c commit 1a2a879
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions gno.land/pkg/gnoweb/static/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ label > img {

code {
white-space: pre-wrap;
overflow-wrap: anywhere;
}
/*** COMPOSITION ***/
.container {
Expand Down
8 changes: 7 additions & 1 deletion gno.land/pkg/gnoweb/static/js/highlight.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,10 @@ className:"number",variants:[{begin:e.C_NUMBER_RE+"[i]",relevance:1
},e.C_NUMBER_MODE]},{begin:/:=/},{className:"function",beginKeywords:"func",
end:"\\s*(\\{|$)",excludeEnd:!0,contains:[e.TITLE_MODE,{className:"params",
begin:/\(/,end:/\)/,endsParent:!0,keywords:n,illegal:/["']/}]}]}}})()
;hljs.registerLanguage("go",e)})();
;hljs.registerLanguage("go",e)})();/*! `json` grammar compiled for Highlight.js 11.5.1 */
(()=>{var e=(()=>{"use strict";return e=>({name:"JSON",contains:[{
className:"attr",begin:/"(\\.|[^\\"\r\n])*"(?=\s*:)/,relevance:1.01},{
match:/[{}[\],:]/,className:"punctuation",relevance:0},e.QUOTE_STRING_MODE,{
beginKeywords:"true false null"
},e.C_NUMBER_MODE,e.C_LINE_COMMENT_MODE,e.C_BLOCK_COMMENT_MODE],illegal:"\\S"})
})();hljs.registerLanguage("json",e)})();
14 changes: 11 additions & 3 deletions gno.land/pkg/gnoweb/static/js/marked.min.js

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions gno.land/pkg/gnoweb/static/js/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,26 @@ function renderUsernames(raw) {
}

function parseContent(source) {
marked.setOptions({ gfm: true });
const { markedHighlight } = globalThis.markedHighlight;
const { Marked } = globalThis.marked;
const markedInstance = new Marked(
markedHighlight({
langPrefix: 'language-',
highlight(code, lang, info) {
if (lang === "json") {
try {
code = JSON.stringify(JSON.parse(code), null, 2);
} catch {}
}
const language = hljs.getLanguage(lang) ? lang : 'plaintext';
return hljs.highlight(code, { language }).value;
}
})
);
markedInstance.setOptions({ gfm: true });
const doc = new DOMParser().parseFromString(source, "text/html");
const contents = doc.documentElement.textContent;
return marked.parse(contents);
return markedInstance.parse(contents);
}

/*
Expand Down
1 change: 1 addition & 0 deletions gno.land/pkg/gnoweb/views/funcs.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
{{- end -}}

{{- define "js" -}}
<script type="text/javascript" src="/static/js/highlight.min.js"></script>
<script type="text/javascript" src="/static/js/marked.min.js"></script>
<script type="text/javascript" src="/static/js/umbrella.min.js"></script>
<script type="text/javascript" src="/static/js/purify.min.js"></script>
Expand Down

0 comments on commit 1a2a879

Please sign in to comment.