Skip to content

Instantly share code, notes, and snippets.

@33mhz
Created October 6, 2017 13:26
Show Gist options
  • Save 33mhz/05d519cfa7561ff86a81db66940695c8 to your computer and use it in GitHub Desktop.
Save 33mhz/05d519cfa7561ff86a81db66940695c8 to your computer and use it in GitHub Desktop.
reverse markdown from pnut entities

Setup

This is pseudocode.

post has been retrieved from the API.

Be sure to make substrings and other string operations by "multibyte" divisions.

Operation

text = post.text;
entities = post.entities;

diff = 0;
entities.links = reverse entities.links;
for entities.links as link
  if link.amended_len exists
    text = (substring of text from position 0 to (link.pos + diff)) and '[' and link.text and '](' and link.link and ')' and (substring of text from (link.pos+diff+link.amended_len) until the end)
    diff += link.amended_len - link.len;

text is the original, now.

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