Skip to content

Instantly share code, notes, and snippets.

@soffes
Last active August 29, 2015 13:57
Show Gist options
  • Save soffes/9475833 to your computer and use it in GitHub Desktop.
Save soffes/9475833 to your computer and use it in GitHub Desktop.
Just a little excerpt of a post I'm going to write on Whiskey's engine when it comes out.

Given the following Markdown:

# Some Header

This is a paragraph with a [link](http://usewhiskey.com).

iA Writer says there are 12 words. Byword says there are 11. Whiskey, a Markdown text editor I’m working on, says there are 9. Count them.

There's actually 9:

Some Header This is a paragraph with a link
1    2      3    4  5 6         7    8 9

Whiskey gets it right because it understands the Markdown you type. To Whiskey, that Markdown looks like this internally:

<MDKNode: 0x600000296800; type = root; range = {0, 72}>
   | <MDKNode: 0x600000298d80; type = header; range = {0, 15}>
   |    | <MDKNode: 0x600000484560; type = text; range = {2, 11}>
   | <MDKNode: 0x600000282940; type = paragraph; range = {15, 57}>
   |    | <MDKNode: 0x600000282440; type = text; range = {15, 27}>
   |    | <MDKNode: 0x600000096530; type = link; range = {42, 29}>
   |    |    | <MDKNode: 0x600000296df0; type = text; range = {43, 4}>
   |    | <MDKNode: 0x6000004818b0; type = text; range = {71, 1}>

It knows where the text is inside of Markdown elements. When it counts words, it knows to exclude the # in the header and other Markdown things since that's not actually the text you're typing. This is just one of many powerful things Whiskey can do since it understands what you're typing.

For the nerds, Whiskey parses everything into an abstract syntax tree and does computations on the tree. Word count is just one of the many things the AST comes in handy for.


If you’re interested in hearing more about Whiskey, follow @soffes on Twitter and sign up to get notified when Whiskey launches at usewhiskey.com.

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