hexojs/hexo-renderer-pandoc

How to new a line with Enter?

lxmymjr opened this issue · 6 comments

I wonder how to new a line with ENTER in hexo-renderer-pandoc?
I used to write my blog with the style that new a line with ENTER.
But recently I update my hexo-theme-next theme, it now support hexo-renderer-pandoc for the Mathjax.
When I switch my hexo renderer to pandoc, I find that making a newline need two space and one break.
It is cumbersome to change my Blog source code. Therefore, can you help me customize hexo-renderer-pandoc to support making newline with ENTER?
Thanks!

  1. The question being raised is how to preserve ENTER as new line.
    Pandoc Markdown uses two ENTER's as line break, and a single ENTER has no semantic meaning.
    What is desired is to preserve a single ENTER as a line break.

Referring to Pandoc Manual, what is desired can be achieved by passing --wrap=preserve (or possibly --wrap=none if the former one is not what is wanted).
With this plugin, this can be achieved by adding the following to _config.yml of the site (not that of the theme):

``` yaml pandoc: extra: - wrap=preserve ```

Above is wrong, below is correct. note the double quotes and the colon.

pandoc:
  extra:
    - "wrap=preserve":
  1. I see in the history of this issue report, it has also once been desired to use GitHub Flavoured Markdown (GFM).

To do this, Pandoc require -f gfm to be passed as command line argument.
This is not yet possible to this plugin, as we hard coded -f markdown and only allowing users to set extensions.

Before we change anything, it is possible to hack this by adding the following to the site _config.yml:

pandoc:
  extensions:
    - _github

This trick will result in -f markdown_github being passed to Pandoc.
Please note that markdown_github is deprecated by Pandoc.
It behaves almost like -f gfm, except extensions may differ.

The next question to ask is whether to allow users to even specify Pandoc input/output format.
A small step would be allowing users to choose which kind of Markdown format the input is, and still render to HTML.
In that case, we do not need to change anything in Hexo renderer registering.
We can further allow users to even specify a set of (InputFileExtension, PandocInputFormat, PandocOutputFormat, OutputFileExtension), and register the renderer according to such configuration.

I foresee that either approach would make it less obvious to the user when they are doing something wrong.
One example being a later registered renderer overwrite a previously registered one, and there would be no indication of that behaviour by Hexo at all.
So if we choose to do this, we need to verify the configuration and give appropriate messages to make the plugin more usable.

However, I do not own this project, and I am not sure how far the owner would like to go along this line.
@wzpan Would you like to share your opinions?
If @wzpan like the idea, I can implement it.
And could @wzpan also give me privilege to update the package to npm?

@lxmymjr if I have satisfied you, could you close this issue?

@RichardYan314 Thank you for your reply.
I have added the code

pandoc:
  extra:
    - wrap=preserve

in the _config.yml file (Blog's one, not the theme's one).
However, when I run hexo -g, it raises the error:

Error: pandoc exited with code 2: Unknown option --0.
Unknown option --1.
Unknown option --2.
Unknown option --3.
Unknown option --4.
Unknown option --5.
Unknown option --6.
Unknown option --7.
Unknown option --8.
Unknown option --9.
Unknown option --10.
Unknown option --11.
Unknown option --12.
Unknown option --replaceAll.
Try pandoc --help for more information.
    at ChildProcess.<anonymous> (D:\Study\Blog\node_modules\hexo-renderer-pandoc\index.js:94:20)
    at ChildProcess.emit (events.js:189:13)
    at maybeClose (internal/child_process.js:970:16)
    at Socket.stream.socket.on (internal/child_process.js:389:11)
    at Socket.emit (events.js:189:13)
    at Pipe._handle.close (net.js:597:12)

Need I modify the code in index.js? if needed, how?
When I print the variable 'extra' in the index.js, it shows:
--0,w,--1,r,--2,a,--3,p,--4,=,--5,p,--6,r,--7,e,--8,s,--9,e,--10,r,--11,v,--12,e
which is very strange.
Maybe something wrong in the line 21 to line 31 of index.js:

    if(config.extra) {
      for(var e in config.extra) {
	var eoption = config.extra[e];
        for (var key in eoption){
          extra.push('--' + key);
          if(eoption[key]!=null) {
            extra.push(eoption[key]); 
          }
	}
      }
    }

I tried to modify these codes, but failed.
Can you help me fix this problem, thank you!

Sorry, my bad. It should be

pandoc:
  extra:
    - "wrap=preserve":

note the double quotes and the colon.

The code except a k-v list, but without the colon, the foreach iterates over index and element. So the colon is needed.

@RichardYan314 It can be complied now, but no change in my blog.
I have confirmed my Pandoc is newest:

$ pandoc --version
pandoc.exe 2.7.2
Compiled with pandoc-types 1.17.5.4, texmath 0.11.2.2, skylighting 0.7.7

@RichardYan314 Following your idea, I have found an alternative way to achieve my goal.
I use:

pandoc:
  extensions:
    - "+hard_line_breaks"

This works well for me.
Reference: Pandoc: no line wrapping when converting to HTML

Thanks for your generous help!

@lxmymjr I am glad to know you have found a solution.

For others reading this, the hard_line_breaks extension causes all newlines within a paragraph to be interpreted as hard line breaks instead of spaces.

I am closing this issue.