From 05733b6bc92db37b8c7bbee792f40125714aa020 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sat, 12 Nov 2016 22:41:43 +0100 Subject: [PATCH] Remove attachment links, as we are already showing them. --- package.json | 1 + src/modules/statuses.js | 7 ++----- src/services/status_parser/status_parser.js | 15 +++++++++++++++ .../services/status_parser/status_parses.spec.js | 11 +++++++++++ yarn.lock | 14 +++++++++++++- 5 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 src/services/status_parser/status_parser.js create mode 100644 test/unit/specs/services/status_parser/status_parses.spec.js diff --git a/package.json b/package.json index 45c26f2d..66134807 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "dependencies": { "moment": "^2.15.2", "node-sass": "^3.10.1", + "sanitize-html": "^1.13.0", "sass-loader": "^4.0.2", "vue": "^2.0.1", "vue-router": "^2.0.1", diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 8ed2890f..cc5e296c 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -1,6 +1,7 @@ import { reduce, map, slice, last, intersectionBy, sortBy, unionBy, toInteger, groupBy, differenceBy, each, find } from 'lodash' import moment from 'moment' import apiService from '../services/api/api.service.js' +import parse from '../services/status_parser/status_parser.js' export const defaultState = { allStatuses: [], @@ -60,11 +61,7 @@ const addStatusesToTimeline = (addedStatuses, showImmediately, { statuses, visib const statusoid = status.retweeted_status || status statusoid.created_at_parsed = statusoid.created_at - - if (statusoid.parsedText === undefined) { - // statusoid.parsedText = statusParserService.parse(statusoid) - statusoid.parsedText = statusoid.text - } + statusoid.statusnet_html = parse(statusoid.statusnet_html) if (statusoid.nsfw === undefined) { const nsfwRegex = /#nsfw/i diff --git a/src/services/status_parser/status_parser.js b/src/services/status_parser/status_parser.js new file mode 100644 index 00000000..0eb3e75f --- /dev/null +++ b/src/services/status_parser/status_parser.js @@ -0,0 +1,15 @@ +import sanitize from 'sanitize-html' + +export const removeAttachmentLinks = (html) => { + return sanitize(html, { + allowedTags: false, + allowedAttributes: false, + exclusiveFilter: ({ tag, attribs: { class: klass } }) => tag === 'a' && klass.match(/attachment/) + }) +} + +export const parse = (html) => { + return removeAttachmentLinks(html) +} + +export default parse diff --git a/test/unit/specs/services/status_parser/status_parses.spec.js b/test/unit/specs/services/status_parser/status_parses.spec.js new file mode 100644 index 00000000..e6d185f8 --- /dev/null +++ b/test/unit/specs/services/status_parser/status_parses.spec.js @@ -0,0 +1,11 @@ +const example = '
@dwmatiz https://social.heldscal.la/attachment/159853
' + +import { removeAttachmentLinks } from '../../../../../src/services/status_parser/status_parser.js' + +describe('statusParser.removeAttachmentLinks', () => { + const exampleWithoutAttachmentLinks = '
@dwmatiz
' + it('removes attachment links', () => { + const parsed = removeAttachmentLinks(example) + expect(parsed).to.eql(exampleWithoutAttachmentLinks) + }) +}) diff --git a/yarn.lock b/yarn.lock index 5aaf7542..189019c6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2614,7 +2614,7 @@ html-webpack-plugin@^2.8.1: pretty-error "^2.0.2" toposort "^1.0.0" -htmlparser2@^3.8.2: +htmlparser2@^3.8.2, htmlparser2@^3.9.0: version "3.9.2" resolved "https://registry.yarnpkg.com/htmlparser2/-/htmlparser2-3.9.2.tgz#1bdf87acca0f3f9e53fa4fcceb0f4b4cbb00b338" dependencies: @@ -4778,6 +4778,10 @@ regex-cache@^0.4.2: is-equal-shallow "^0.1.3" is-primitive "^2.0.0" +regexp-quote@0.0.0: + version "0.0.0" + resolved "https://registry.yarnpkg.com/regexp-quote/-/regexp-quote-0.0.0.tgz#1e0f4650c862dcbfed54fd42b148e9bb1721fcf2" + regexpu-core@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/regexpu-core/-/regexpu-core-1.0.0.tgz#86a763f58ee4d7c2f6b102e4764050de7ed90c6b" @@ -4961,6 +4965,14 @@ samsam@1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/samsam/-/samsam-1.1.2.tgz#bec11fdc83a9fda063401210e40176c3024d1567" +sanitize-html: + version "1.13.0" + resolved "https://registry.yarnpkg.com/sanitize-html/-/sanitize-html-1.13.0.tgz#4ee17cbec516bfe32f2ce6686a569d7e6b4f3631" + dependencies: + htmlparser2 "^3.9.0" + regexp-quote "0.0.0" + xtend "^4.0.0" + sass-graph@^2.1.1: version "2.1.2" resolved "https://registry.yarnpkg.com/sass-graph/-/sass-graph-2.1.2.tgz#965104be23e8103cb7e5f710df65935b317da57b"