Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Nov 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 5 additions & 3 deletions src/renderer/components/TimelineSpace/Contents/Cards/Toot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 11 additions & 6 deletions src/renderer/utils/tootParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
24 changes: 20 additions & 4 deletions test/mocha/tootParser.spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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">
Expand All @@ -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')
})
})
})
Expand Down