-
Notifications
You must be signed in to change notification settings - Fork 620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
syntax highlight in html #621
Comments
Not native to vimwiki, as far as I know. You could include something like highlight.js, I'd think, but I've never used it. |
You can do this as follows using highlight.js (you could also use Google's prettify but highlight.js is easier to work with):
In <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/styles/default.min.css" /> At the end of <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/highlight.min.js"></script>
<script type="text/javascript">
document.querySelectorAll('pre').forEach(block => hljs.highlightBlock(block));
</script> Now tell Vimwiki that you want to use this template: let g:vimwiki_list = [{'path': '/path/to/wikis', 'template_path': '/path/to/directory/containing/default.tpl'}] Highlight.js will detect syntax automatically. So in your Vimwiki files, you just need to use the You can also "hint" at the language to help highlight.js by using @EinfachToll @Nudin or any other maintainers: is there any desire to add support for this natively, at least through the inclusion of some kind of global option (i.e. |
I am a new maintainer, and I think this would be nice to have natively. However, I will defer to @EinfachToll and @Nudin until I get a better sense for what flavor of changes they think are appropriate, so you may want to hold off on this for now, while we handle the current PR's. |
Hey, I really like to have syntax-highlithing in the HTML-export. But I also think we shouldn't add JS to the default template since most users won't need it. And I think we should handle this and the math-rendering consistently. |
@Nudin I think setting it in an option (default |
I completely agree with @Nudin that this shouldn't be a default. I personally do everything I can to avoid a dependency on javascript and it's easy enough to add in as needed. When I initially looked at handling the syntax highlighting without highlight.js I decided to try rendering color markup for the code snippets through a python library (the name escapes me now) but I haven't actually sat down to get it working end to end and so I didn't suggest it. The .js solution is probably the easiest anyway. |
A while ago I looked at integrating highlight(1) with VimWiki. I prefer that solution since it generates HTML tags for the code highlighting during the build step, not relying on javascript. The problem I found with adding the feature was that the current HTML creator doesn't keep track of what's in the code block, but rather just creates start and end tags for them (I believe). |
For a non JS implementation how easy/difficult would it be to pass the codeblock through vim's builtin |
I might be able to add this functionality using the tree-sitter cli. @hq6 Do you think I should tackle this as my first issue? @EinfachToll @Nudin Is this a proper solution to this? |
Would love some guidance on enhancing this but was thinking something along the lines of this, I guess the command and the output could be options for the user so that it is not limited to tree-sitter, or pygmentize.: diff --git a/autoload/vimwiki/html.vim b/autoload/vimwiki/html.vim
index 0da3d81..80dbbff 100644
--- a/autoload/vimwiki/html.vim
+++ b/autoload/vimwiki/html.vim
@@ -1628,6 +1628,27 @@ function! s:convert_file(path_html, wikifile) abort
let html_lines = s:html_insert_contents(html_lines, ldest) " %contents%
+ let i = 0
+ while i < len(html_lines)
+ if html_lines[i] =~ '^<pre type=.\+>'
+ let type = split(split(split(html_lines[i], 'type=')[1], '>')[0], '\s\+')[0]
+ let start = i + 1
+ let cur = start
+
+ while html_lines[cur] !~ '^<\/pre>'
+ let cur += 1
+ endwhile
+
+ let tmp = ('tmp'. split(system('mktemp -p . --suffix=.' . type, 'silent'), 'tmp')[-1])[:-2]
+ call system('echo ' . shellescape(join(html_lines[start:cur - 1], '\n')) . ' > ' . tmp)
+ call system('pygmentize -f html ' . tmp . ' -o ' . tmp . '.html')
+ let html_out = system('cat ' . tmp . '.html')
+ call system('rm ' . tmp . ' ' . tmp . '.html')
+ let i = cur
+ let html_lines = html_lines[0:start - 2] + split(html_out, '\n') + html_lines[cur + 1:]
+ endif
+ let i += 1
+ endwhile
call writefile(html_lines, path_html.htmlfile)
let done = 1 |
This is probably a reasonable approach, but it would definitely need to be behind some configuration; we can't assume the external dependencies. Also keep in mind that it shouldn't be OS-dependent. |
(I do note there's code to check OS, though I haven't worked with it enough to comment much on it. grep for |
so the todo list looks something like this:
|
Seems right. Probably off-by-default. |
Fixed by #1181 1181 |
* issue#621: html highlighter * docs for issue#621 * removing windows support for issue#621 Co-authored-by: Tinmarino <tinmarino@gmail.com>
Is it possible for the exported html to have syntax highlighting for the embedded code fragment?
The text was updated successfully, but these errors were encountered: