Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

HL JS Code Blocks are missing new lines and indentation #20

Closed
sebromero opened this issue Jul 22, 2021 · 7 comments
Closed

HL JS Code Blocks are missing new lines and indentation #20

sebromero opened this issue Jul 22, 2021 · 7 comments

Comments

@sebromero
Copy link

I'm using version 1.0.1 and after converting a HL JS code block of this format

<pre class="hljs arduino"><code><span><span class="hljs-comment">// Created by Alberto Perro - Officine Innesto</span>
</span><span><span class="hljs-comment">//</span>
</span><span><span class="hljs-comment">// Read integer from a modbus device holding registers</span>
</span><span><span class="hljs-keyword">float</span> readCurrent(){        
</span><span>    <span class="hljs-keyword">float</span> ampere = <span class="hljs-number">0.</span>;    
</span><span>    <span class="hljs-comment">//next command format the request and send it over RS485        </span>
</span><span>    <span class="hljs-built_in">if</span> (!ModbusRTUClient.<span class="hljs-built_in">requestFrom</span>(<span class="hljs-number">0x01</span>, HOLDING_REGISTERS, <span class="hljs-number">0x0016</span>, <span class="hljs-number">2</span>))
</span><span>    {  

...

I get the following rendering:

```
// Created by Alberto Perro - Officine Innesto//
// Read integer from a modbus device holding registersfloat readCurrent(){ float ampere = 0.; //next command format the request and send it over RS485 if (!ModbusRTUClient.requestFrom(0x01, HOLDING_REGISTERS, 0x0016, 2)) { //Error Handling Serial.print("failed to read voltage! "); Serial.println(ModbusRTUClient.lastError()); } else { uint16_t word1 = ModbusRTUClient.read(); //reading MSW from buffer uint16_t word2 = ModbusRTUClient.read(); //reading LSW from buffer int32_t milliamp = word1 << 16 | word2; //join words to retreive int ampere = milliamp/1000.0; //convert to ampere } return ampere;}
```

Is there an easy way to fix this?

@nonara
Copy link
Collaborator

nonara commented Jul 23, 2021

Thanks for the report! I'll have a look this weekend.

@sebromero
Copy link
Author

Thank you! Also, the language specifier isn't extracted from the code. Let me know if you need a more complete code example to test it.

@sebromero
Copy link
Author

Actually, the example is from here: https://create.arduino.cc/projecthub/123325/monitor-your-energy-bill-via-modbus-mkr-wifi-1010-and-rs485-814e5e
Great work by the way! Love this package <3

@crisdias
Copy link

I had a similar problem here and getting rid of all the <span>s solved it.

// Created by Alberto Perro - Officine Innesto
//
// Read integer from a modbus device holding registers
float readCurrent(){
float ampere = 0.;
//next command format the request and send it over RS485
if (!ModbusRTUClient.requestFrom(0x01, HOLDING_REGISTERS, 0x0016, 2)) {

@nonara
Copy link
Collaborator

nonara commented Jul 26, 2021

Should be sorted in v1.1.0! Let me know if it doesn't work for you.

@sebromero
Copy link
Author

Thanks for the update! Maybe I'm doing something wrong but it still doesn't work for me with 1.1.0. Test:

import nhm from 'node-html-markdown';
const { NodeHtmlMarkdown, NodeHtmlMarkdownOptions } = nhm;
const data = `<html><body><pre class="hljs arduino"><code><span><span class="hljs-comment">// Created by Alberto Perro - Officine Innesto</span>
</span><span><span class="hljs-comment">//</span>
</span><span><span class="hljs-comment">// Read integer from a modbus device holding registers</span>
</span><span><span class="hljs-keyword">float</span> readCurrent(){        
</span><span>    <span class="hljs-keyword">float</span> ampere = <span class="hljs-number">0.</span>;    
</span><span>    <span class="hljs-comment">//next command format the request and send it over RS485        </span>
</span><span>    <span class="hljs-built_in">if</span> (!ModbusRTUClient.<span class="hljs-built_in">requestFrom</span>(<span class="hljs-number">0x01</span>, HOLDING_REGISTERS, <span class="hljs-number">0x0016</span>, <span class="hljs-number">2</span>))
</span><span>    {         
</span><span>    <span class="hljs-comment">//Error Handling          </span>
</span><span>    
</span><span>    <span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">print</span>(<span class="hljs-string">"failed to read voltage! "</span>);    
</span><span>    <span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">println</span>(ModbusRTUClient.lastError());         
</span><span>    
</span><span>    } <span class="hljs-built_in">else</span> {        
</span><span>        uint16_t word1 = ModbusRTUClient.<span class="hljs-built_in">read</span>(); <span class="hljs-comment">//reading MSW from buffer</span>
</span><span>        uint16_t word2 = ModbusRTUClient.<span class="hljs-built_in">read</span>(); <span class="hljs-comment">//reading LSW from buffer</span>
</span><span>        int32_t milliamp = word1 &lt;&lt; <span class="hljs-number">16</span> | word2;   <span class="hljs-comment">//join words to retreive int</span>
</span><span>        ampere = milliamp/<span class="hljs-number">1000.0</span>;                 <span class="hljs-comment">//convert to ampere    </span>
</span><span>    }        
</span><span><span class="hljs-built_in">return</span> ampere;}
</span></code></pre></body></html>`;

console.log(NodeHtmlMarkdown.translate(data));

Prints:

// Created by Alberto Perro - Officine Innesto
//
// Read integer from a modbus device holding registers
float readCurrent(){     float ampere = 0.;     //next command format the request and send it over RS485 
    if (!ModbusRTUClient.requestFrom(0x01, HOLDING_REGISTERS, 0x0016, 2)) {     //Error Handling 
    
    Serial.print("failed to read voltage! ");     Serial.println(ModbusRTUClient.lastError());     
 } else {  uint16_t word1 = ModbusRTUClient.read(); //reading MSW from buffer
 uint16_t word2 = ModbusRTUClient.read(); //reading LSW from buffer
 int32_t milliamp = word1 << 16 | word2; //join words to retreive int
 ampere = milliamp/1000.0; //convert to ampere 
 } return ampere;}

@nonara nonara reopened this Jul 31, 2021
@nonara nonara closed this as completed in af79995 Aug 3, 2021
@nonara
Copy link
Collaborator

nonara commented Aug 3, 2021

Sorry about that. That was my mistake!

I wrote it to use the non-trimmed text, and that part got wiped out. I had a feeling something was missing when rewriting that section. Should be corrected in the latest version.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants