Hacker News new | past | comments | ask | show | jobs | submit login
Markdown.css – make HTML look like plain-text (mrcoles.com)
420 points by mrcoles on Feb 13, 2013 | hide | past | favorite | 89 comments



Firstly this is awsome!

As much as I like kittens... This adds the markdown for images too:

    .markdown img {
      display: inline;
      content: "";
    }
    .markdown img:before {
      content: "![" attr(alt) "](" attr(src) ")";
      display: inline;
      color: #333333;
    }
The trick to make it work with images is the `content: "";` on the img element to stop the browser from treating it as an image.

EDIT:

See it here: http://jsfiddle.net/F2mjs/

Anyone know how to make the link clickable?


It worked for me on Chrome 24, Opera 12, Safari 6, but it doesn't appear to work on Firefox 18.


You're using opera, right?

This was already in my code, but I commented out, maybe I should just let it be in there real quick before opera changes to webkit: https://github.com/mrcoles/markdown-css/blob/016bf0863867aed...

If not opera, lmk what browser you're using.


This is working in Chrome 24.

Oh, yes it is. Its in the .less file, I had not spotted that.

EDIT:

You need `content: "";` in the less file and it should work.


Oh, interesting. Chrome does play nice with that.

Also, it looks like maybe you have to make the img content "", in order for the :before to work—otherwise it doesn't show up, at least in chrome, and with that demo it appears as nothing at all in firefox. It's a cruel world…

In the bookmarklet (included at top of demo) I do an extra step to make images inside links smaller and appear inside the link markdown—not what you were getting at, but it is a little something something.


Wow. Was just looking into this myself. Great find.


Nicely done sir. Pull request ahoy!


This is cool :) I do a similar thing on my site[1]to make it look like Org-mode files (which is what I use to write it). I enjoy the fact that so much processing goes into making it look just like it did to begin with :p

1: http://almostobsolete.net


very clever, particularly how the width of the equals signs below an h1 is made to match the text width using hidden overflow:

https://github.com/mrcoles/markdown-css/blob/0d00981143172aa...


The magic is the quirks in how `display: table-cell` makes it like a block, but only as wide as the text, then the pseudo element with equals signs is positioned absolutely inside. Fortunately, most browsers allow this, but firefox doesn't let things be positioned absolute inside a `table-cell`, so I make FF just fall back to `display: block` https://github.com/mrcoles/markdown-css/blob/master/markdown...


interesting!

in the same vein as the h1 equals, i like the blockquote brackets implementation as well:

https://github.com/mrcoles/markdown-css/blob/0d00981143172aa...


yeah, the \A makes a newline and the `white-space: pre` makes that work like a <br>


That was definitely the most impressive bit for me too. I thought, how on earth was that done? Very clever.

It's a shame it doesn't work for copy-paste, but honestly, it's probably even easier to write a reverse-markdown script than to figure out how to do it with CSS!


It's interesting and seems well implemented but I don't understand the use case? Why would you need this?

If you want plain text inside HTML isn't what the <pre> tag is for?


Mostly because it's fun in a nerdy way, and it was an interesting challenge to implement.

I decided to use it for a demos page on my site: http://mrcoles.com/demo/

Also, there's a bookmarklet at the top of the markdown-css demo that let's you apply the markdown styles to any page, which is like a harder-to-read version of readability or a quick way to see how well layed out the markup is on any page.


As a developer you may not want it - but my friends who are copywriters, editors and content strategists will LOVE this. A simple way to grab existing web content as markdown, which many are already using to write and edit their content.


The problem (at least for me on FF18/Win7) is that you can't just copy/paste the converted markdown, since it is just a display trick, not actual parsed and generated raw text.


Exactly.

I had made a converter that used XSLT and produced actual selectable and copy-able (?) markdown text; didn't gain much traction; I'll post it again to see if anyone's interested.


Here is my version of a Markdown converter:

http://markitdown.medusis.com/

Converts any rich text to Markdown (vanilla Markdown, but selectable).


Same with FF18 on other platforms. (Iceweasel 18 on Parabola)


But this doesn't convert HTML to Markdown (there's already pandoc for that), it displays it as if it were Markdown. It's a clever example of what you can do with CSS, as well as perhaps poking fun at the current popularity of systems like Markdown which artificially restrict us to the IO capabilities of 70s VTs.


i already posted this lower in the thread, but try http://leeoniya.github.com/reMarked.js/


Interesting - I made the assumption that what I saw could be copied. Thanks for the pandoc reference. I wasn't aware of that.


Here's another way to convert HTML to Markdown: http://www.lowerelement.com/Geekery/XML/XHTML-to-Markdown.ht...


XHTML, your 90s are calling…


That makes more sense to me now. Thinking about it I see it being useful to help with some currently painful tasks like converting old wiki content that has a non-standard wiki markup syntax (or no markup at all) into markdown.


Well, maybe you want the HTML formatting without the styles or all the CSS classes. Say you wanted to do something like grab all the docs off a website and then apply your own styling or put them on an internal reference. I can see that being a good use case to allow that for open documentation.


Maybe it's like "Readbility" and those other ways of de-cluttering a web page or standardizing your style as a reader? If you have a browser that is set up to let you choose a style sheet for display, you now have a "distraction-free reading environment."

I, for one, will be adding this to http://ristrettolo.gy and http://combinators.info. Markup -> HTML -> Markup. It's a crazy world!


Markup -> HTML -> Markup

    s/Markup/Markdown/   # ?


note that it's markdown, not just plain text.

i think it's mostly just for fun though / as an impressive CSS trick.


This is awesome. Projects like this make me appreciate the fortitude of Markdown. John has made a project that essentially got it right on the first try[1], with very few patches or additions. In a world of software development where I'm running `gem update` all the time, it's nice to have something like Markdown which really knocked it out of the park in one try. I don't have to check the docs to know if the syntax for URL linking changed, and your implementation isn't going to be any different from mine. It's nice to have a standard like that everyone can rally behind.

[1]: I'm aware of projects like MMD, which AFAIK are primarily add-ons to the original Markdown syntax. I also realize that Markdown isn't perfect: Jeff Atwood has a good write-up on this from late 2009 -- http://www.codinghorror.com/blog/2009/12/responsible-open-so...


shameless plug: client-side (or node) html > markdown, like actual conversion

demo: http://leeoniya.github.com/reMarked.js/

repo: https://github.com/leeoniya/reMarked.js


Nice! I am working on an editor (based on content-editable) which translates to markdown. If your script handles invalid html I might as well use yours.


it relies on the client's html > DOM conversion, so in a sense it does. but it does not have a self-contained html parser.


Is that available as a bookmarklet, where you can select content on an existing page and extract markdown to the clipboard?


it'd be fairly easy to make one, but i imagine it would have limited utility since the entire content of the page is rarely what you want. how would you go about selecting which portion you're interested in? also, i'm not certain a bookmarklet would have direct clipboard access due to security, i could be wrong tho.

feel free to open up an issue on github to discuss the idea and implementation details further.


This may have changed in the last few years, but I believe you can simply do "window.getSelection()" in your bookmarklet to access the currently selection for Firefox/WebKit browsers.


might be worth trying. however, i'm still not convinced the presented use-case would be useful rather than just cool.


Sadly you're almost certainly right. Most of the places I could see wanting to reverse-engineer the displayed HTML into Markdown are actually sites using their own hackish, inconsistently rendered output. You'd end up needing per-site tweaks and that'd kind of render the whole thing unworkable and pointless.


cool!


I still wonder why Markdown is so popular while reStructuredText is not. I find the latter much easier to read, it looks better in both plain and rendered format (and rst2pdf makes nice docs).


Well, one thing that definitely isn't helping is its horrible name. And as evidence, the very first thing on their site:

"'reStructuredText' is ONE word, not two!"

I'm definitely never going to type out that entire word, so now do I abbreviate it to reST? or ReST like some people on the site seem to be doing? It'll just get confused with REST in Google searches...


Markdown is 'lighter' and aimed at the more general case: churning out lightly-formatted text quickly, with as few 'rules' as possible, with page layout concerns addressed by templates defined elsewhere.

reStructuredText is 'heavier' and only really an improvement over Markdown if you're trying to embed page layout into the text. Which fewer people are.


You are right. I think I prefer reST because it produces documents. It's not a quick way to add markup, it's a quick way to structure a document (and provide links, sections, inline code etc).


Personally I found the RST documentation quite inaccessible for doing simple things.


Heh, I did something a year ago to do the opposite - take a plain markdown file and give it some styles.

http://kevinburke.bitbucket.org/markdowncss/

HN discussion: http://news.ycombinator.com/item?id=2827892



Wow, that's really similar with very similar approaches. The internet is a great way to find out you're not very original. I'll add a link to that project from mine.


Kudos.


Pretty cool. Would it also be possible to include the markdown markup when copying the text?


the after and before pseudo elements are kind of weird. I think for it to work with copy-paste I'd have to include some JS to look at the selection, reverse markdown it, and then get it into your clipboard.


Quite frankly that would be the most common useful use case for this, in my opinion. In any case this is some great css hacking!


Agreed, would love to use this to generate markdown!


Finally. A proper use-case for displaying content with the :before selector.


Then it's only fitting that the use-case is completely useless.


IIRC, I saw :before used to great effect in print stylesheets for turning links into plain-text URLs.


Personally I think to add an icon (using an icon font or possibly a data uri) is a great (and proper) use-case for displaying content with either the :before or :after selector.


This is not a "proper" use case. I am not able to understand why one would want to "view" Markup in Markdown?


I think could be interesting for Phrack as they are (or should be) modernizing their e-zine format.

In that way, the old school boys can still keep their pure-ASCII, and others may wish to apply some other CSS to make it more pleasant to read.


Just submitted a pull request to auto-increment the ordered list numbering so it doesn't repeat 1. over and over.

https://github.com/mrcoles/markdown-css/pull/4


I'm pretty sure that was an intentional design choice to make the text look more like Markdown. Ordered lists increment by default — they went out of their way to disable that.


This is nice. The one use case its missing for me is copy/paste with formatting. Currently I use:

    $ pandoc -f html -t markdown http://example.org/page.htm > page.mdwn


If you're interested in this, you should also know about Showdown.js, a library that converts markdown into HTML client-side: https://github.com/coreyti/showdown

For an example use, view the source of http://dergachev.github.com/pin-screenshot-bookmarklet


I'm much better at HTML than Markdown. I totally could use this when I'm forced to write Markdown.


You can't, actually. Use Pandoc.


Awesome project. Is there anything that can actually convert HTML to Markdown though?


The wonderful pandoc (http://johnmacfarlane.net/pandoc/README.html#general-options) can, along with many other things.


I use pandoc[1] for this.

  curl -s example.com | pandoc -f html -t markdown -
[1]: http://johnmacfarlane.net/pandoc/


This works too:

    pandoc -t markdown http://example.com


Nice. Didn't know pandoc can directly read from an URL, even easier.



Pretty neat and cool! Seeing this i got reminded how awesome markdown is and i just played around with my vim markdown plugin after long time.


Cool, but the numbered list has everything lablled as '1.' for me: ff esr 10.0.12


Yeah, technically markdown doesn't need the numbers to be incremented, so I thought it'd be more fun to explicitly not use counters. Maybe it's too confusing though. There's a discussion about it here: https://github.com/mrcoles/markdown-css/issues/1


UPDATE: I ended up merging that in and making the ones toggle-able, and then added a toggle link into the demo so you can see both http://mrcoles.com/demo/markdown-css/


>in markdown, the actual numbers don’t matter

That's to be expected, you make ordered lists like that in markdown :)


Ha, I didn't know you could just use any number until just now, makes sense, doh! Thanks.


yea, its a good case using just 30. item1 999. item2 2. item3 for numbered lists

but it can possibly "break" in some later versions of markdown, as said by the man himself:

"If you do use lazy list numbering, however, you should still start the list with the number 1. At some point in the future, Markdown may support starting ordered lists at an arbitrary number." -- John Gruber / Daring Fireball (http://daringfireball.net/projects/markdown/syntax#list)


I find this clever. Use cases aside, it's fun.


I don't understand this, Markdown is meant for writing.

Why don't you just output real markdown with something like Jekyll, Markdown Server (node) or Showdown?


It’s meant as an interesting CSS hack to make markup look like markdown, but still function as regular HTML (including having things like links still work). There are thousands of things that compile markdown to HTML, that's not at all what this does. Try clicking on the different tabs to show the different states or the demo link to the left of them—maybe that'll make more sense?


I still don't get it, why should I make something look like markdown when it can be actual markdown. Links work in markdown, you can eve use something like maruku to have html elements (divs, id's etc) in actual markdown.

Markdown is portable, fake markdown isn't.


This is hilarious. Thank you :)


Just curious, but what version of Opera did you get `:before` & `:after` working on? I was just trying this out on the latest Opera and couldn't get it to work.


Works for me on opera 12.14 on Mac OS X. What version and OS are you using?


I'm also using 12.14 on OS X 10.7.5

Opera/9.80 (Macintosh; Intel Mac OS X 10.7.5) Presto/2.12.388 Version/12.14


What is it that you're not seeing working? screen shot of how it looks to me in opera: http://i.imgur.com/7te5TWW.png


I meant regarding the image, but I looked at the code and realized you didn't include it at all. Sorry about the confusion.


Does anybody else think we've come full circle now?

I don't see how this is at all useful. Just upload a .md file.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: