Skip to content

Use existing sanistiser (html parser) as cleaner to track body clicks - #694

Merged
gtrufitt merged 4 commits into
masterfrom
gtrufitt/sanitise-and-track-body-clicks
Mar 2, 2020
Merged

gtrufitt merged 4 commits into
masterfrom
gtrufitt/sanitise-and-track-body-clicks

Conversation

@gtrufitt

@gtrufitt gtrufitt commented Aug 7, 2019 •

Copy link
Copy Markdown
Contributor

What does this change?

We have the existing htmlSanitiser used in AMP. I've moved this to shared lib so that we can use it in web to add data-link-name to anchors. This is nice well-defined solution for modifying (read: cleaning) markup and uses htmlparser2 under the hood (which I was going to use anyway).

image

Why?

We need to add this attribute for Ophan to track body links.

Link to supporting Trello card

https://trello.com/c/eagQlGEf/639-track-onward-journeys-in-ophan

@guardian/dotcom-platform

@PRBuilds

PRBuilds commented Aug 7, 2019

Copy link
Copy Markdown

PRbuilds results:

💚 AMP validation
amp-report.txt

LightHouse Reporting

--automated message

@gtrufitt

gtrufitt commented Aug 8, 2019 •

Copy link
Copy Markdown
Contributor Author

UNBLOCKED

blocked until we can move forward with a CS solution that doesn't require hydrating the whole world. cc/ @AWare @SiAdcock @nicl @GHaberis (maybe we can get the band back together with the rest of the team to try and hash out a way forward, Nic has some thoughts written already)

@gtrufitt
gtrufitt force-pushed the gtrufitt/sanitise-and-track-body-clicks branch from 9de0b1e to d9dda3e Compare February 27, 2020 09:58
@gtrufitt
gtrufitt force-pushed the gtrufitt/sanitise-and-track-body-clicks branch from d1b9d26 to d9dda3e Compare February 27, 2020 12:33

@oliverlloyd oliverlloyd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for a hard earned, persistent effort in getting this PR through!

Would it be possible to replace escapeData.tsx with this lib?

Comment on lines +42 to +58
const sanitiserOptions = {
// Defaults: https://www.npmjs.com/package/sanitize-html#what-are-the-default-options
allowedTags: false, // Leave tags from CAPI alone
allowedAttributes: false, // Leave attributes from CAPI alone
transformTags: {
a: (tagName: string, attribs: {}) => ({
tagName, // Just return anchors as is
attribs: {
...attribs, // Merge into the existing attributes
...{
'data-link-name': 'in body link', // Add the data-link-name for Ophan to anchors
},
},
}),
},
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the scopes or use cases here are but could this live in the sanitise-html lib? Or will the options be different each time? (Assuming there will be next times)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, maybe we could have base options defined at definition and extend them by spreading locally at use?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to replace escapeData.tsx with this lib?

Yeah maybe! Will put a card in to review.

Or will the options be different each time?

I think it's likely it will be sufficiently different each time that base options wouldn't be that useful. This is basically giving us all the info (work on a, keep the attributes, add data-link-name) local to the transformation which I guess is our preferred approach now.

@gtrufitt
gtrufitt merged commit 6db3005 into master Mar 2, 2020
@gtrufitt
gtrufitt deleted the gtrufitt/sanitise-and-track-body-clicks branch March 2, 2020 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants