Skip to content

Instantly share code, notes, and snippets.

@kn1cht
Last active June 23, 2019 00:41
Show Gist options
  • Save kn1cht/911ca1e4c3e92c636a603dce940eae99 to your computer and use it in GitHub Desktop.
Save kn1cht/911ca1e4c3e92c636a603dce940eae99 to your computer and use it in GitHub Desktop.
embed-tweet
scrapbox.PopupMenu.addButton({
title : 'Embed Tweet',
onClick : text => {
const convertContent = (content, indent) => {
const replaced = content
.replace(/<a href="(https?:.*?)">(.*?)<\/a>/g, '[$1 $2]')
.replace(/<br>/g, `\n${indent}`);
const elem = document.createElement('div');
elem.innerHTML = replaced;
return indent + elem.innerText;
}
const tweetRegex = /(?<spaces>[\t ]*)<blockquote[\w"\-= ]*><p[\w"\-= ]*>(?<content>.*)<\/p>&mdash;(?<author>.*)(?<link><a.*>)<\/blockquote>[\n\t ]*<script.*?><\/script>/;
const match = text.match(tweetRegex);
if(!match) return;
const {spaces, content, author, link} = match.groups;
const ind = `${spaces}>`;
return text.replace(tweetRegex, convertContent(content, ind) + '\n' + convertContent(`\t—${author} ${link}`, ind));
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment