Wikify hashtags

This commit is contained in:
jeremy@jermolene.com 2022-11-14 08:51:18 +00:00
parent 710e51fe04
commit 06520c8994
4 changed files with 54 additions and 21 deletions

View File

@ -76,7 +76,7 @@ TwitterArchivist.prototype.loadArchive = async function(options) {
var tags = ["$:/tags/Tweet"];
// Accumulate the replacements/insertions to the text as an array of {startPos:,endPos:,fnTransform:}
var modifications = [];
// Mentions
// Modifications for mentions
var mentions = [];
$tw.utils.each(tweet.tweet.entities.user_mentions,function(mention) {
var title = "Tweeter - " + mention.id_str;
@ -90,24 +90,43 @@ TwitterArchivist.prototype.loadArchive = async function(options) {
name: mention.name
});
modifications.push({
startPos: mention.indices[0],
endPos: mention.indices[1],
startPos: parseInt(mention.indices[0],10),
endPos: parseInt(mention.indices[1],10),
fnTransform: function(text) {
return "<$link to=\"" + title + "\">" +
$tw.utils.htmlEncode(text.substring(mention.indices[0],mention.indices[1])) +
"</$link>";
"</$link>";
}
});
});
// URLs
// Modifications for URLs
$tw.utils.each(tweet.tweet.entities.urls,function(urlInfo) {
modifications.push({
startPos: urlInfo.indices[0],
endPos: urlInfo.indices[1],
startPos: parseInt(urlInfo.indices[0],10),
endPos: parseInt(urlInfo.indices[1],10),
fnTransform: function(text) {
return "<a href=\"" + $tw.utils.htmlEncode(urlInfo.expanded_url) + "\" rel=\"noopener noreferrer\" target=\"_blank\">" +
return "<a href=\"" + urlInfo.expanded_url + "\" rel=\"noopener noreferrer\" target=\"_blank\">" +
$tw.utils.htmlEncode(urlInfo.display_url) +
"</a>";
"</a>";
}
});
});
// Modifications for hashtags
$tw.utils.each(tweet.tweet.entities.hashtags,function(hashtag) {
var title = "#" + hashtag.text;
tags.push(title);
wiki.addTiddler({
title: title,
hashtag: hashtag.text,
tags: "$:/tags/Hashtag"
});
modifications.push({
startPos: parseInt(hashtag.indices[0],10),
endPos: parseInt(hashtag.indices[1],10),
fnTransform: function(text) {
return "<$link to=\"" + title + "\">" +
"#" + $tw.utils.htmlEncode(hashtag.text) +
"</$link>";
}
});
});
@ -117,25 +136,24 @@ TwitterArchivist.prototype.loadArchive = async function(options) {
});
// Apply the modifications in reverse order
var rawText = tweet.tweet.full_text,
posText = rawText.length,
posText = 0,
chunks = [];
for(var modificationIndex=modifications.length-1; modificationIndex>=0; modificationIndex--) {
var modification = modifications[modificationIndex];
// Process any text after the modification
if(posText > modification.endPos) {
chunks.push($tw.utils.htmlEncode(rawText.substring(modification.endPos,posText)));
$tw.utils.each(modifications,function(modification) {
// Process any text before the modification
if(modification.startPos > posText) {
chunks.push($tw.utils.htmlEncode(rawText.substring(posText,modification.startPos)));
}
// Process the modification
chunks.push(modification.fnTransform(rawText));
// Adjust the position
posText = modification.startPos;
}
posText = modification.endPos;
});
// Process any remaining text
if(posText > 0) {
chunks.push($tw.utils.htmlEncode(rawText.substring(0,posText)));
if(posText < rawText.length) {
chunks.push($tw.utils.htmlEncode(rawText.substring(posText)));
}
// Concatenate the chunks and replace newlines with <br>
var text = chunks.reverse().join("").replace("\n","<br>");
var text = chunks.join("").replace("\n","<br>");
// Create the tweet tiddler
var tiddler = {
title: "Tweet - " + tweet.tweet.id_str,

View File

@ -206,6 +206,17 @@ No Twitter Archives are currently loaded
\define show-tweeter-mentions()
<$list filter="[tag[$:/tags/Tweet]tag<currentTiddler>]">
<$macrocall $name="show-tweet" archive=<<currentTiddler>> title=<<currentTiddler>>/>
<$macrocall $name="show-tweet" title=<<currentTiddler>>/>
</$list>
\end
\define show-hashtag()
<a href={{{ [{!!user_id}addprefix[https://twitter.com/intent/user?user_id=]] }}} rel="noopener noreferrer" target="_blank">View on Twitter</a>
<$macrocall $name="skinny-tabs" tabNames="show-hashtag-tweets" tabCaptions="Tweets" defaultTab="show-hashtag-tweets" state=<<qualify "$:/state/skinny-tabs/hashtag-tweets">>/>
\end
\define show-hashtag-tweets()
<$list filter="[tag[$:/tags/Tweet]tag<currentTiddler>]">
<$macrocall $name="show-tweet" title=<<currentTiddler>>/>
</$list>
\end

View File

@ -0,0 +1,3 @@
title: $:/plugins/tiddlywiki/twitter-archivist/template/hashtag
<<show-hashtag>>

View File

@ -5,3 +5,4 @@ list-before:
[tag[$:/tags/Tweet]then[$:/plugins/tiddlywiki/twitter-archivist/template/tweet]]
[tag[$:/tags/TwitterArchive]then[$:/plugins/tiddlywiki/twitter-archivist/template/archive]]
[tag[$:/tags/Tweeter]then[$:/plugins/tiddlywiki/twitter-archivist/template/tweeter]]
[tag[$:/tags/Hashtag]then[$:/plugins/tiddlywiki/twitter-archivist/template/hashtag]]