Costa Costa - 5 months ago 30
Node.js Question

Using jade as a template language for plain text

I'm using jade to craft html emails. You know, personalize the 'to' field, things like that. I'd like to provide a plain text version as well. Is there any way to compile jade to plain text?

Sid Sid
Answer

If jade -> html -> plain is an acceptable solution you could do:

var htmlToText = require('html-to-text');
var jade = require('jade');

const jadeTemplate = `doctype html
html(lang="en")
  head
    title= pageTitle
    script(type='text/javascript').
      if (foo) {
         bar(1 + 5)
      }
  body
    h1 Jade - node template engine
    #container.col
      if youAreUsingJade
        p You are amazing
      else
        p Get on it!
      p.
        Jade is a terse and simple
        templating language with a
        strong focus on performance
        and powerful features.`;





var html = jade.compile(jadeTemplate, {})({});
var text = htmlToText.fromString(html, {
    wordwrap: 130
});



document.getElementById('text').value = text;
Comments