Skip to content

Instantly share code, notes, and snippets.

@kn1cht

kn1cht/script.js

Last active Jun 23, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.