From 06520c8994124a7685c8232e8dc34468082e201f Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Mon, 14 Nov 2022 08:51:18 +0000 Subject: [PATCH] Wikify hashtags --- .../tiddlywiki/twitter-archivist/archivist.js | 58 ++++++++++++------- .../tiddlywiki/twitter-archivist/macros.tid | 13 ++++- .../twitter-archivist/template-hashtag.tid | 3 + .../view-template-body-cascade.tid | 1 + 4 files changed, 54 insertions(+), 21 deletions(-) create mode 100644 plugins/tiddlywiki/twitter-archivist/template-hashtag.tid diff --git a/plugins/tiddlywiki/twitter-archivist/archivist.js b/plugins/tiddlywiki/twitter-archivist/archivist.js index e1ca29a42..e0ffe2d52 100644 --- a/plugins/tiddlywiki/twitter-archivist/archivist.js +++ b/plugins/tiddlywiki/twitter-archivist/archivist.js @@ -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])) + - ""; + ""; } }); }); - // 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 "" + + return "" + $tw.utils.htmlEncode(urlInfo.display_url) + - ""; + ""; + } + }); + }); + // 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) + + ""; } }); }); @@ -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
- var text = chunks.reverse().join("").replace("\n","
"); + var text = chunks.join("").replace("\n","
"); // Create the tweet tiddler var tiddler = { title: "Tweet - " + tweet.tweet.id_str, diff --git a/plugins/tiddlywiki/twitter-archivist/macros.tid b/plugins/tiddlywiki/twitter-archivist/macros.tid index a45592c10..74dd530d9 100644 --- a/plugins/tiddlywiki/twitter-archivist/macros.tid +++ b/plugins/tiddlywiki/twitter-archivist/macros.tid @@ -206,6 +206,17 @@ No Twitter Archives are currently loaded \define show-tweeter-mentions() <$list filter="[tag[$:/tags/Tweet]tag]"> - <$macrocall $name="show-tweet" archive=<> title=<>/> + <$macrocall $name="show-tweet" title=<>/> + +\end + +\define show-hashtag() +View on Twitter +<$macrocall $name="skinny-tabs" tabNames="show-hashtag-tweets" tabCaptions="Tweets" defaultTab="show-hashtag-tweets" state=<>/> +\end + +\define show-hashtag-tweets() +<$list filter="[tag[$:/tags/Tweet]tag]"> + <$macrocall $name="show-tweet" title=<>/> \end diff --git a/plugins/tiddlywiki/twitter-archivist/template-hashtag.tid b/plugins/tiddlywiki/twitter-archivist/template-hashtag.tid new file mode 100644 index 000000000..2193d84e5 --- /dev/null +++ b/plugins/tiddlywiki/twitter-archivist/template-hashtag.tid @@ -0,0 +1,3 @@ +title: $:/plugins/tiddlywiki/twitter-archivist/template/hashtag + +<> diff --git a/plugins/tiddlywiki/twitter-archivist/view-template-body-cascade.tid b/plugins/tiddlywiki/twitter-archivist/view-template-body-cascade.tid index ffc18c3d4..f6a52d0e4 100644 --- a/plugins/tiddlywiki/twitter-archivist/view-template-body-cascade.tid +++ b/plugins/tiddlywiki/twitter-archivist/view-template-body-cascade.tid @@ -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]]