Giter Club home page Giter Club logo

Comments (20)

valmormn avatar valmormn commented on July 19, 2024 12

Soo.. paragraphs are not supported?

Fuck that shit

from snarkdown.

kwiat1990 avatar kwiat1990 commented on July 19, 2024 11

I have come up with something like this:

function snarkdownEnhanced(markdown) {
  return markdown
    .split(/(?:\r?\n){2,}/)
    .map((l) =>
      [" ", "\t", "#", "-", "*", ">"].some((char) => l.startsWith(char))
        ? snarkdown(l)
        : `<p>${snarkdown(l)}</p>`
    )
    .join("\n");

With the addition of ">" (markdown for a blockquote) all the redundant paragraphs are gone. So basically lists and blockquotes won't produce empty paragraphs anymore.

from snarkdown.

RobbieTheWagner avatar RobbieTheWagner commented on July 19, 2024 8

I would love for this to support paragraphs. This breaks all the formatting of everything. I get a ton of newlines I do not want in my output.

from snarkdown.

gribnoysup avatar gribnoysup commented on July 19, 2024 3

Yay! I'll take a look ;)

from snarkdown.

dangzo avatar dangzo commented on July 19, 2024 2

That's my solution to the problem (taking inspiration from the comments on this issue):

let html = '<p>';
contentMd.split('\n\n').forEach(mdChunk => {
    html += `<p>${snarkdown(mdChunk)}</p>`;
});
html += '</p>';

Works well in most cases (for as far as my tested went). Conversion of a single newline into <br /> is till not supported. So, something like:

Hello,
I'm in a new line

Would still output:

<p>Hello, I'm in a new line</p>

from snarkdown.

nicobrinkkemper avatar nicobrinkkemper commented on July 19, 2024 1

Soo.. paragraphs are not supported?

from snarkdown.

pReya avatar pReya commented on July 19, 2024 1

Just another +1 here. Would really love to have support for proper paragraphs. Love the minimal approach of this library, but now I need to switch to larger parsers, just because I need support for <p> tags 😒

from snarkdown.

kwiat1990 avatar kwiat1990 commented on July 19, 2024 1

I think that br is not what one is expecting. I mean, plain text should be enclose with a proprietary HTML tag, in this case a pair of <p></p>. Otherwise we make some damage to markup's semantics. I would love to see this enhancement as there is absolutely no alternative to snarkdown in terms of size.

EDIT: Sadly your solution produces in my markdown empty paragraphs.

from snarkdown.

mesqueeb avatar mesqueeb commented on July 19, 2024 1

The following works, but it will break when a sentence starts with a bold word. I was able to improve it by adding some spaces inside the [].some check:

BEFORE

function snarkdownEnhanced(markdown) {
  return markdown
    .split(/(?:\r?\n){2,}/)
    .map((l) =>
      [" ", "\t", "#", "-", "*", ">"].some((char) => l.startsWith(char))
        ? snarkdown(l)
        : `<p>${snarkdown(l)}</p>`
    )
    .join("\n");

changed "-", "*", ">" with "- ", "* ", "> "

AFTER

function snarkdownEnhanced(markdown) {
  return markdown
    .split(/(?:\r?\n){2,}/)
    .map((l) =>
      [" ", "\t", "#", "- ", "* ", "> "].some((char) => l.startsWith(char))
        ? snarkdown(l)
        : `<p>${snarkdown(l)}</p>`
    )
    .join("\n");

from snarkdown.

developit avatar developit commented on July 19, 2024

That could be useful, and actually the block processor seems to be already set up to handle this. Worth looking into.

from snarkdown.

patrickpietens avatar patrickpietens commented on July 19, 2024

+1

from snarkdown.

woudsma avatar woudsma commented on July 19, 2024

+1

from snarkdown.

ollicle avatar ollicle commented on July 19, 2024

This is a surprising omission for a Markdown parser, but perhaps nearly a feature. I have long thought markdown for strictly inline markup would be useful. For example when the output is to be contained in an existing template paragraph. A fork to remove the support for headings and Snarkdown would be it…

from snarkdown.

developit avatar developit commented on July 19, 2024

@ollicle what about code blocks and quotes?

from snarkdown.

developit avatar developit commented on July 19, 2024

currently they are transformed to line breaks.

from snarkdown.

Jonarod avatar Jonarod commented on July 19, 2024

I know it will not correctly solve the issue, but as a fallback, if other people comes to have the same problem, I ended up forcing <p></p> and <br> before snarkdown conversion. As snarkdown correctly accepts HTML, it should render correct paragraphs. It goes like:

var myMarkdown = 'I am a line but I should be in my own paragraph\n\n*Here* starts another paragraph, and here is a line break\nI come**after**the line break but still in the same paragraph\n\n'

// replacing double lines \n\n with </p><p> then single lines \n with <br>
myMarkdown.replace(/\n\n/g, "</p><p>").replace(/\n/g, "<br>")

//Now converting to markdown and enclosing the output with <p></p>. 
// snarkdown preserves previously forced HTML tags
var html = '<p>' + snarkdown(myMarkdown)  + '</p>'

// Do whatever with the variable "html"

All in all, this creates empty trailing <p></p> tags at the end of the output... but still I have correct markdown for really cheap thanks to snarkdown... with an empty p tag at the end: I guess I can live with that until a better solution comes :)

EDIT: This works only on some contexts. The "hack" breaks quite quickly when we try to use basic block markdown like h1 etc... It will mess the whole thing. Use only for some inline markdown needs.

from snarkdown.

Jonarod avatar Jonarod commented on July 19, 2024

I have been through the code trying to find a way to sneak into it and make support for <p></p> tags.

I tried these options:

  1. Create a new regex that would basically match any paragraph: everything that starts with other thing than commons markdown blocks (blockquotes, lists, fences...). But inevitably leads to a loop, as hello is a paragraph returning hello which in turns matches a paragraph as well...
    Actually for post-reference, this crazy regex would correctly match paragraphs:
/(^(?![-*+] |[-_*:`=]{3,}|\>|\t|\ {4}|\s|\||#{1,6}\s|\d+(?=\. )|<)(?:[\w\W])+?(?=\n(?:\n|\>|[-+*]\ .|\*{3,}\n|#{1,6}\s|[`:]{3,})|(\n[=-])))/gm

Here I put together a test to see it in action with several edge cases.

  1. After that, I tried to stick with the regex approach, but using some conditionals to prevent further parses... But I found myself in the situation where _hello_ would output <p>_hello_</p> without parsing nested content.

  2. I changed my view on regex, and tried to do something AFTER snarkdown's output. I saw a bunch of <br /> that may be used to get an entry point of <p>. Here again, I can't seem to find the good path to adding correct <p></p> tags. In fact, After HTML is produced, I could barely surround "lonely" text with <p></p>, but there I was left with lone <em>, <strong> and inline friends hanging. In fact, it would have been easier with some AST to tell where blocks/inline/text start or ends.

Now I am new to javascript and programming in general, so I may have skipped some BIG OBVIOUS scheme: @developit what do you think would be the best approach here ?

You say:

That could be useful, and actually the block processor seems to be already set up to handle this. Worth looking into.

What did you mean ?
Any help would do :)

from snarkdown.

manix84 avatar manix84 commented on July 19, 2024

+1

from snarkdown.

aradalvand avatar aradalvand commented on July 19, 2024

I mean, plain text should be enclose with a proprietary HTML tag

Totally agree, I was disappointed to see that Snarkdown doesn't convert "markdown paragraphs into HTML paragraphs"!
You'd certainly expect that by default.

from snarkdown.

swyxio avatar swyxio commented on July 19, 2024

Just to help out anyone finding this issue, kwiat1990's solution above worked, where Jonarod's solution breaks with H tags or code blocks

from snarkdown.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.