Skip to content

Instantly share code, notes, and snippets.

@NV
Created January 21, 2010 12:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NV/282770 to your computer and use it in GitHub Desktop.
Save NV/282770 to your computer and use it in GitHub Desktop.
function Markdown(value) {
this.value = value || '';
this.length = this.value.length;
};
Markdown.prototype = new String;
// Instance methods
Markdown.prototype.toString = Markdown.prototype.valueOf = function(){return this.value};
Markdown.prototype.toHTML = function(){return Markdown.decode(this)};
// Static methods
Markdown.decode = function(){};
// Example:
m = new Markdown('*italic*');
m.toHTML(); // '<i>italic</i>'
Markdown.decode('*italic*'); // '<i>italic</i>'
@tomek-he-him
Copy link

If you have access to ES5, this would be better (replacing line 3):

Object.defineProperty(this, 'length', {get: function () { return this.value.length; }});

You then get:

m.length;  // » 8
m.value = '**bolder**';
m.length;  // » 10

@pwormer
Copy link

pwormer commented Jan 26, 2015

I'm doubly confused by: "Markdown.decode = function(){};" --a function without arguments.
In the first place it is called with the parameter "this". In the second place decode() is not a native String method.

Presumably the reader is expected to write "decode()" him/herself and in the body convert "arguments[0].value" to HTML?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment