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"]; var tags = ["$:/tags/Tweet"];
// Accumulate the replacements/insertions to the text as an array of {startPos:,endPos:,fnTransform:} // Accumulate the replacements/insertions to the text as an array of {startPos:,endPos:,fnTransform:}
var modifications = []; var modifications = [];
// Mentions // Modifications for mentions
var mentions = []; var mentions = [];
$tw.utils.each(tweet.tweet.entities.user_mentions,function(mention) { $tw.utils.each(tweet.tweet.entities.user_mentions,function(mention) {
var title = "Tweeter - " + mention.id_str; var title = "Tweeter - " + mention.id_str;
@ -90,24 +90,43 @@ TwitterArchivist.prototype.loadArchive = async function(options) {
name: mention.name name: mention.name
}); });
modifications.push({ modifications.push({
startPos: mention.indices[0], startPos: parseInt(mention.indices[0],10),
endPos: mention.indices[1], endPos: parseInt(mention.indices[1],10),
fnTransform: function(text) { fnTransform: function(text) {
return "<$link to=\"" + title + "\">" + return "<$link to=\"" + title + "\">" +
$tw.utils.htmlEncode(text.substring(mention.indices[0],mention.indices[1])) + $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) { $tw.utils.each(tweet.tweet.entities.urls,function(urlInfo) {
modifications.push({ modifications.push({
startPos: urlInfo.indices[0], startPos: parseInt(urlInfo.indices[0],10),
endPos: urlInfo.indices[1], endPos: parseInt(urlInfo.indices[1],10),
fnTransform: function(text) { 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) + $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 // Apply the modifications in reverse order
var rawText = tweet.tweet.full_text, var rawText = tweet.tweet.full_text,
posText = rawText.length, posText = 0,
chunks = []; chunks = [];
for(var modificationIndex=modifications.length-1; modificationIndex>=0; modificationIndex--) { $tw.utils.each(modifications,function(modification) {
var modification = modifications[modificationIndex]; // Process any text before the modification
// Process any text after the modification if(modification.startPos > posText) {
if(posText > modification.endPos) { chunks.push($tw.utils.htmlEncode(rawText.substring(posText,modification.startPos)));
chunks.push($tw.utils.htmlEncode(rawText.substring(modification.endPos,posText)));
} }
// Process the modification // Process the modification
chunks.push(modification.fnTransform(rawText)); chunks.push(modification.fnTransform(rawText));
// Adjust the position // Adjust the position
posText = modification.startPos; posText = modification.endPos;
} });
// Process any remaining text // Process any remaining text
if(posText > 0) { if(posText < rawText.length) {
chunks.push($tw.utils.htmlEncode(rawText.substring(0,posText))); chunks.push($tw.utils.htmlEncode(rawText.substring(posText)));
} }
// Concatenate the chunks and replace newlines with <br> // 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 // Create the tweet tiddler
var tiddler = { var tiddler = {
title: "Tweet - " + tweet.tweet.id_str, title: "Tweet - " + tweet.tweet.id_str,

View File

@ -206,6 +206,17 @@ No Twitter Archives are currently loaded
\define show-tweeter-mentions() \define show-tweeter-mentions()
<$list filter="[tag[$:/tags/Tweet]tag<currentTiddler>]"> <$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> </$list>
\end \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/Tweet]then[$:/plugins/tiddlywiki/twitter-archivist/template/tweet]]
[tag[$:/tags/TwitterArchive]then[$:/plugins/tiddlywiki/twitter-archivist/template/archive]] [tag[$:/tags/TwitterArchive]then[$:/plugins/tiddlywiki/twitter-archivist/template/archive]]
[tag[$:/tags/Tweeter]then[$:/plugins/tiddlywiki/twitter-archivist/template/tweeter]] [tag[$:/tags/Tweeter]then[$:/plugins/tiddlywiki/twitter-archivist/template/tweeter]]
[tag[$:/tags/Hashtag]then[$:/plugins/tiddlywiki/twitter-archivist/template/hashtag]]