How to automatically style HTML output of kramdown converter?


Tags: html,css,kramdown

Problem :

I'm writing notes in kramdown, then using the kramdown converter to get an html file to read locally (not on a server).

$ kramdown notes.txt > notes.html

My workflow is to continually update notes.txt, then render notes.html quickly to view it.

I want to style the notes, so I made a CSS file notes_style.css that formats the HTML body:

body {
...
}

I added a <head> tag to notes.txt and put in a link to my stylesheet. Then I put <body> tags around the contents of notes.txt:

<head>
  <link rel="stylesheet" type="txt/css" href="themes/notes_style.css" />
</head>
<body>
  ...
</body>

I then realized that, since kramdown ignores HTML blocks, nothing in <body> is converted to HTML. How am I supposed to style the contents of <body> while being able to convert the kramdown to HTML? I don't want to manually add in <body> tags every time I use kramdown.

I suspect that something's wrong with my workflow, but not sure what the right way to do it is.



Solution :

Kramdown has the parse_block_html option that causes it to parse the contents of HTML elements. You can use it like this:

$ kramdown --parse-block-html notes.txt > notes.html

Alternatively, Kramdown also looks for a special markdown attribute on HTML elements. If it is set to '1', then Kramdown will parse the contents of that element as markdown. So you could do this (you need the quotes around 1, Kramdown’s HTML parser is quite strict):

<head>
  <link rel="stylesheet" type="txt/css" href="themes/notes_style.css" />
</head>
<body markdown='1'>
Content here will be parsed _as Markdown_.
</body>

In both cases, your body content needs to be left aligned as normal, you can’t indent it as you might naturally do writing HTML nested inside an element.


    CSS Howto..

    How do I maintain modularity with mixins?

    How can we wrap the line based on border size of windows browser?

    How can I apply CSS selector to attribute values being case sensitive?

    How do I make part of a style element a function? (IE: color:$random instead of color:red)

    How to offset div columns in CSS grid system

    How to position footer correctly with css?

    How to find everything that is not matching a regular expression

    How to change the CSS on the last clicked div

    How to optimize this css code? [closed]

    How do I align my navbar so it's permanently in the middle of my white box (without padding etc)?

    How can I resize to fit html elements horizontally using CSS?

    How to make this working Java-script more efficient

    How to understand CSS Background-Position coordinates

    How browser auto resize website to mobile browse?

    How to show divs inline. css html

    How can I put an input element on the same line as its label?

    How to style HTML select option hover and selected option effects

    CSS select + select How to get first select

    section to be trigger of show/hide content by click html/css only

    How to I make the width of my CSS wrapper relative to the size of the monitor?

    How to change css property of the particular selection text in a input field using javascript

    How to center this flip card example?

    How to replace a Font Awesome icon within link text with a different icon on link click?

    How to delete css right or bottom on drag

    How do I center my logo image to another div in CSS horizontally and vertically?

    How to move the Google Chart's annotation to the top or anywhere with CSS?

    How do i set the HTML tag's “background” property with JS, still allowing other properties from a CSS file to be unchanged?

    How to show ellipsis for long cell values in kendo ui grid?

    How can I add a border INSIDE this element?

    How to improve this code to combine/compress CSS using PHP [closed]