fix: Fix tag parser in tootParser for Pleroma's tag

This commit is contained in:
AkiraFukushima 2018-11-03 12:35:03 +09:00
parent ca2fb46488
commit e6fd65e936
5 changed files with 44 additions and 19 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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') {

View File

@ -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(`<html><head></head><body>
<div class="toot">
@ -33,8 +33,24 @@ I released Whalebird version 2.4.1. In version 2.4.0, Whalebird supports streami
</html>`)).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(`<html><head></head><body>
<div class="toot">
<p>
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. <br /><a href="https://github.com/h3poteto/whalebird-desktop/releases/tag/2.4.1">https://github.com/h3poteto/whalebird-desktop/releases/tag/2.4.1</a><br /><a id="tag" class="hashtag" href="https://pleroma.io/tag/whalebird">#<span>Whalebird</span></a>
</p>
</div>
</body>
</html>`)).window.document
const target = doc.getElementById('tag')
it('should find', () => {
const res = findTag(target)
assert.strictEqual(res, 'whalebird')
})
})
})