hexojs / hexo

A fast, simple & powerful blog framework, powered by Node.js.
https://hexo.io
MIT License
39.51k stars 4.86k forks source link

Beautified html code for code snippets get weird new lines in the code #1601

Closed vitto closed 4 years ago

vitto commented 8 years ago

I'm using hexo to create post pages with code snippets, I'm doing it via node cause I'm writing a module to create style guides and I'd like to mantain a customizable HTML instead to use an article generated by markdown code only.

hexo = new Hexo(process.cwd(), {
    debug: false,
    config: fromModule('_config.yml')
});

hexo.init().then(function(){
    var postData;
    for (var i = 0; i < widgetFiles.length; i += 1) {
        postData = formatter.toHexo(widgetFiles[i]);
        postData.content = widget.toMarkdown(widgetFiles[i], conf);
        // tracing HTML snippet
        console.log(postData.docs[0].htmlSnippet);
        hexo.post.create(postData, true);
    }
});

My only problem is the htmlSnippet var which goes into hexo.post.create method well formatted as the example below but comes badly formatted on the ejs template, where I'd like to use hexo markdown plugin.

Before hexo.post.create(postData, true); using console.log(postData[i][0].htmlSnippet); as the upper example

<a href="#" class="button-social button-social--github button-social--with-effects button-social--small">
    <div class="button-social__icon"><i class="fa fa-github-alt"></i></div>
    <div class="button-social__network">github</div>
</a>

Then after the post is created, on ejs template, using <% console.log(post.docs[0].htmlSnippet) %> give these strange additional new lines in the code:

<a href="#" class="button-social button-social--github button-social--with-effects button-social--small">

    <div class="button-social__icon"><i class="fa
fa-github-alt"></i></div>

    <div class="button-social__network">github</div>

</a>

I also noticed that if I use <%- markdown(post.docs[0].htmlSnippet) %> from the ejs template it doesn't show code line numbers: https://github.com/vitto/a-pollo/blob/master/hexo/themes/a-pollo/layout/_partial/widget-body.ejs#L22

If I use <%- markdown(post.content) %> it renders the block with also line number and everything well formatted as I expected: https://github.com/vitto/a-pollo/blob/master/lib/markdown.js#L49


How can I solve the wrong code snippet indentation problem?


If you'd like to test it by your own:

First time installation:

npm install a-pollo
cd a-pollo
./node_modules/.bin/bower install
./node_modules/.bin/gulp frontsize:build

Then, just:

node index.js

To switch the configurations, in a-pollo.yml just change the boolean value

use_markdown: false
Xuanwo commented 8 years ago

@vitto Sorry for delay, but your link is 404

vitto commented 8 years ago

@Xuanwo sorry, I've updated the link

stevenjoezhang commented 4 years ago

Closing due to inactivity, please re-open if it is still outstanding.