-
-
Save yokky21/23364f06f3ab0a8c220d6da295ffab78 to your computer and use it in GitHub Desktop.
Mastodon2Twitter without boost and reply. Truncation strings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* mstdn2birdsite.ts - The filter code of "Mastodon → Twitter" | |
* Written in 2017 by nullkal <[email protected]> | |
* | |
* To the extent possible under law, the author(s) have dedicated all copyright | |
* and related and neighboring rights to this software to the public domain | |
* worldwide. This software is distributed without any warranty. | |
* You should have received a copy of the CC0 Public Domain Dedication along | |
* with this software. If not, see | |
* <http://creativecommons.org/publicdomain/zero/1.0/>. | |
*/ | |
type HandleFunc = (match: Array<string>) => string; | |
interface Handler { | |
pattern: RegExp; | |
func: HandleFunc; | |
}; | |
class FeedFilter { | |
private handlers: Handler[]; | |
constructor() { | |
this.handlers = []; | |
} | |
public addHandler(pattern: RegExp, func: HandleFunc) { | |
this.handlers.push({ pattern, func }); | |
} | |
public exec() { | |
for (const i in this.handlers) { | |
const handler = this.handlers[i]; | |
const match = handler.pattern.exec(Feed.newFeedItem.EntryTitle); | |
if (match !== null) { | |
Twitter.postNewTweet.setTweet(handler.func(match)); | |
return; | |
} | |
} | |
Twitter.postNewTweet.skip(); | |
} | |
}; | |
function formatContent(content: string) { | |
var str = content | |
.replace(/<br\s*\/?>/g, "\n") | |
.replace(/<.*?>/g, ''); | |
// Truncation | |
if(str.length > 116){ | |
content = str.slice(0,114) + '……'; | |
}else{ | |
content = str; | |
} | |
return content; | |
} | |
/* | |
function formatContent(content: string) { | |
return content | |
.replace(/<br:\s*\/?>/g, "\n") | |
.replace(/<.*?>/g, ''); | |
} | |
*/ | |
let f = new FeedFilter(); | |
f.addHandler(/^New status by /, (match) => { | |
const content = formatContent(Feed.newFeedItem.EntryContent); | |
const url = Feed.newFeedItem.EntryUrl; | |
if (/((?:[^\w]|^)[@@])(\w+)/.test(content)) { | |
Twitter.postNewTweet.skip(); | |
} | |
return `${content} ${url}`; | |
}); | |
/*f.addHandler( | |
/^\w+ shared a status by (\w+(?:@[a-z0-9\.\-]+[a-z0-9]+))/, | |
(match) => { | |
const originalPoster = Feed.newFeedItem.EntryUrl; | |
const content = formatContent(Feed.newFeedItem.EntryContent); | |
return `BT ${originalPoster}\n${content}`; | |
} | |
);*/ | |
f.exec(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment