Giter Club home page Giter Club logo

Comments (5)

lionel-rowe avatar lionel-rowe commented on July 19, 2024 12

Workaround:

const snarkdownEnhanced = (md: string) => {
    const htmls = md
        .split(/(?:\r?\n){2,}/)
        .map(l =>
            [' ', '\t', '#', '-', '*'].some(ch => l.startsWith(ch))
                ? snarkdown(l)
                : `<p>${snarkdown(l)}</p>`,
        )

    return htmls.join('\n\n')
}

Note that it will fail for fenced code blocks and probably other edge cases.

from snarkdown.

6vx avatar 6vx commented on July 19, 2024 1

Workaround:

const snarkdownEnhanced = (md: string) => {
    const htmls = md
        .split(/(?:\r?\n){2,}/)
        .map(l =>
            [' ', '\t', '#', '-', '*'].some(ch => l.startsWith(ch))
                ? snarkdown(l)
                : `<p>${snarkdown(l)}</p>`,
        )

    return htmls.join('\n\n')
}

Note that it will fail for fenced code blocks and probably other edge cases.

This was what I needed. Appreciate it.

I understand that the scope of snarkdown is minimal, but personally I just can't imagine ever using a markdown file to make something only one line long. imo Enhanced should be default. But what do I know.

Thanks everyone.

from snarkdown.

bastienrobert avatar bastienrobert commented on July 19, 2024

#13

from snarkdown.

robsonsobral avatar robsonsobral commented on July 19, 2024

#11

from snarkdown.

kohloth avatar kohloth commented on July 19, 2024

Heres a patch (still a bit of a hack) that I put together to wrap all loose text in <p>s. Unlike the solution above, it seems to work fine with fenced code blocks.

export default function(md) {
	// Run snarkdown
	let out = parse(md);

	// Add opening <p>
	if (!out.trim().startsWith('<')) {
		out = `<p>${out}`;
	}

	out = out
		// Replace e.g. "</h5>The..." with "</h5><p>The..."
		.replace(/(<(\/(h(\d))|em|strong|s|div|pre)>)([\s\r\n]){0,}([\w\d])/g, match => {
			const chars = [
				match.slice(0, match.length - 1),
				match.slice(match.length - 1)
			];
			return `${chars[0]}<p>${chars[1]}`
		})

		// Replace <br> with </p><p>
		.replace(/<br \/>/g, '</p><p>')

		// Ensure paragraphs before h-tags end with </p>
		.replace(/([\w\d.:;])([\r\n]){1,}(<((h(\d))|em|strong|s|div|pre)>)/g, (match, paraChars, space, followingEl) => {
			return `${paraChars || ''}</p>${space || ''}${followingEl || ''}`
		})

		// Div fix
		.replace(/<\/p><p><div/g, '</p><div')
		.replace(/<\/div><\/p>/g, '</div>')

		// Strong, em fix
		.replace(/<strong><p>/g, '<strong>')
		.replace(/<em><p>/g, '<em>')

		// Pre fix
		.replace(/<p><pre/g, '<pre')
		.replace(/<\/pre><\/p>/g, '</pre>')

		// Ul fix
		.replace(/<p><ul/g, '<ul')
		.replace(/<\/ul><\/p>/g, '</ul>')

		// Ol fix
		.replace(/<p><ol/g, '<ol')
		.replace(/<\/ol><\/p>/g, '</ol>')

	// Add closing </p>
	if (!out.trim().endsWith('>')) {
		out = `${out}</p>`;
	}

	return out;
}

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.