From e6fd65e936bb9be83a8b00c3d48ad7d2a591f6ed Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Sat, 3 Nov 2018 12:35:03 +0900 Subject: [PATCH] fix: Fix tag parser in tootParser for Pleroma's tag --- .../Contents/Cards/Notification/Favourite.vue | 7 +++--- .../Contents/Cards/Notification/Reblog.vue | 7 +++--- .../TimelineSpace/Contents/Cards/Toot.vue | 8 ++++--- src/renderer/utils/tootParser.js | 17 ++++++++----- test/mocha/tootParser.spec.js | 24 +++++++++++++++---- 5 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/renderer/components/TimelineSpace/Contents/Cards/Notification/Favourite.vue b/src/renderer/components/TimelineSpace/Contents/Cards/Notification/Favourite.vue index b685440b..4321ceb0 100644 --- a/src/renderer/components/TimelineSpace/Contents/Cards/Notification/Favourite.vue +++ b/src/renderer/components/TimelineSpace/Contents/Cards/Notification/Favourite.vue @@ -77,7 +77,7 @@ import { mapState } from 'vuex' import moment from 'moment' import { shell } from 'electron' -import { findAccount, findLink, isTag } from '~/src/renderer/utils/tootParser' +import { findAccount, findLink, findTag } from '~/src/renderer/utils/tootParser' import emojify from '~/src/renderer/utils/emojify' import TimeFormat from '~/src/constants/timeFormat' @@ -153,8 +153,9 @@ export default { } }, tootClick (e) { - if (isTag(e.target, 'favourite')) { - const tag = `/${this.$route.params.id}/hashtag/${e.target.innerText}` + const parsedTag = findTag(e.target, 'favourit') + if (parsedTag !== null) { + const tag = `/${this.$route.params.id}/hashtag/${parsedTag}` this.$router.push({ path: tag }) return tag } diff --git a/src/renderer/components/TimelineSpace/Contents/Cards/Notification/Reblog.vue b/src/renderer/components/TimelineSpace/Contents/Cards/Notification/Reblog.vue index 47010242..8ab5b36c 100644 --- a/src/renderer/components/TimelineSpace/Contents/Cards/Notification/Reblog.vue +++ b/src/renderer/components/TimelineSpace/Contents/Cards/Notification/Reblog.vue @@ -77,7 +77,7 @@ import { mapState } from 'vuex' import moment from 'moment' import { shell } from 'electron' -import { findAccount, findLink, isTag } from '~/src/renderer/utils/tootParser' +import { findAccount, findLink, findTag } from '~/src/renderer/utils/tootParser' import emojify from '~/src/renderer/utils/emojify' import TimeFormat from '~/src/constants/timeFormat' @@ -152,8 +152,9 @@ export default { } }, tootClick (e) { - if (isTag(e.target, 'reblog')) { - const tag = `/${this.$route.params.id}/hashtag/${e.target.innerText}` + const parsedTag = findTag(e.target, 'reblog') + if (parsedTag !== null) { + const tag = `/${this.$route.params.id}/hashtag/${parsedTag}` this.$router.push({ path: tag }) return tag } diff --git a/src/renderer/components/TimelineSpace/Contents/Cards/Toot.vue b/src/renderer/components/TimelineSpace/Contents/Cards/Toot.vue index 6f4db84d..3681337a 100644 --- a/src/renderer/components/TimelineSpace/Contents/Cards/Toot.vue +++ b/src/renderer/components/TimelineSpace/Contents/Cards/Toot.vue @@ -137,7 +137,7 @@ import moment from 'moment' import { shell, clipboard } from 'electron' import { mapState } from 'vuex' -import { findAccount, findLink, isTag } from '~/src/renderer/utils/tootParser' +import { findAccount, findLink, findTag } from '~/src/renderer/utils/tootParser' import DisplayStyle from '~/src/constants/displayStyle' import TimeFormat from '~/src/constants/timeFormat' import emojify from '~/src/renderer/utils/emojify' @@ -241,8 +241,10 @@ export default { } }, tootClick (e) { - if (isTag(e.target, 'toot')) { - const tag = `/${this.$route.params.id}/hashtag/${e.target.innerText}` + const parsedTag = findTag(e.target, 'toot') + if (parsedTag !== null) { + const tag = `/${this.$route.params.id}/hashtag/${parsedTag}` + console.log(tag) this.$router.push({ path: tag }) return tag } diff --git a/src/renderer/utils/tootParser.js b/src/renderer/utils/tootParser.js index d6ada869..73407dc0 100644 --- a/src/renderer/utils/tootParser.js +++ b/src/renderer/utils/tootParser.js @@ -11,22 +11,27 @@ export function findLink (target, parentClass = 'toot') { return findLink(target.parentNode, parentClass) } -export function isTag (target, parentClass = 'toot') { +export function findTag (target, parentClass = 'toot') { if (target.getAttribute('class') && target.getAttribute('class').includes('hashtag')) { - return true + return parseTag(target.href) } // In Pleroma, link does not have class. // So I have to check URL. if (target.href && target.href.match(/^https:\/\/[a-zA-Z0-9-.]+\/(tag|tags)\/.+/)) { - return true + return parseTag(target.href) } if (target.parentNode === undefined || target.parentNode === null) { - return false + return null } if (target.parentNode.getAttribute('class') === parentClass) { - return false + return null } - return isTag(target.parentNode, parentClass) + return findTag(target.parentNode, parentClass) +} + +export function parseTag (tagURL) { + const res = tagURL.match(/^https:\/\/([a-zA-Z0-9-.]+)\/(tag|tags)\/(.+)/) + return res[3] } export function findAccount (target, parentClass = 'toot') { diff --git a/test/mocha/tootParser.spec.js b/test/mocha/tootParser.spec.js index 8cca9b7c..5191bd47 100644 --- a/test/mocha/tootParser.spec.js +++ b/test/mocha/tootParser.spec.js @@ -1,6 +1,6 @@ import assert from 'assert' import { JSDOM } from 'jsdom' -import { findLink, isTag, findAccount } from '../../src/renderer/utils/tootParser' +import { findLink, findTag, findAccount } from '../../src/renderer/utils/tootParser' describe('findLink', () => { context('Pleroma', () => { @@ -21,7 +21,7 @@ I released Whalebird version 2.4.1. In version 2.4.0, Whalebird supports streami }) }) -describe('isTag', () => { +describe('findTag', () => { context('Pleroma', () => { const doc = (new JSDOM(`
@@ -33,8 +33,24 @@ I released Whalebird version 2.4.1. In version 2.4.0, Whalebird supports streami `)).window.document const target = doc.getElementById('tag') it('should find', () => { - const res = isTag(target) - assert.strictEqual(res, true) + const res = findTag(target) + assert.strictEqual(res, 'whalebird') + }) + }) + + context('Mastodon', () => { + const doc = (new JSDOM(` +
+

+I released Whalebird version 2.4.1. In version 2.4.0, Whalebird supports streaming update of Pleroma. But it contains a bug, so it is resolved in version 2.4.1.
https://github.com/h3poteto/whalebird-desktop/releases/tag/2.4.1
#Whalebird +

+
+ +`)).window.document + const target = doc.getElementById('tag') + it('should find', () => { + const res = findTag(target) + assert.strictEqual(res, 'whalebird') }) }) })