Added segment parser logic

refs https://github.com/TryGhost/Team/issues/828

- This is experimental segment extraction logic, more to follow. Alllows to extract arrays of email segments used in the email's HTML content
This commit is contained in:
Naz 2021-06-29 19:10:51 +04:00
parent 2f2c11bbe0
commit e04af28efe
2 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,13 @@
const cheerio = require('cheerio');
const getSegmentsFromHtml = (html) => {
const $ = cheerio.load(html);
const allSegments = $('[data-gh-segment]')
.get()
.map(el => el.attribs['data-gh-segment']);
return allSegments;
};
module.exports.getSegmentsFromHtml = getSegmentsFromHtml;

View File

@ -0,0 +1,37 @@
const should = require('should');
const {getSegmentsFromHtml} = require('../../../../core/server/services/mega/segment-parser');
describe('MEGA: Segment Parser', function () {
it('extracts a single segments used in HTML', function () {
const html = '<div data-gh-segment="status:-free"><p>Plain html with no replacements</p></div>';
const segments = getSegmentsFromHtml(html);
segments.length.should.equal(1);
segments[0].should.equal('status:-free');
});
it('extracts multiple segments used in HTML', function () {
const html = `
<div data-gh-segment="status:-free"><p>Text for paid</p></div>
<div data-gh-segment="status:free"><p>Text for free</p></div>
<div data-gh-segment="status:-free,label.slug:VIP"><p>Text for paid VIP</p></div>
`;
const segments = getSegmentsFromHtml(html);
segments.length.should.equal(3);
segments[0].should.equal('status:-free');
segments[1].should.equal('status:free');
segments[2].should.equal('status:-free,label.slug:VIP');
});
it('extracts no segments from HTML', function () {
const html = '<div data-gh-somethingelse="status:-free"><p>Plain html with no replacements</p></div>';
const segments = getSegmentsFromHtml(html);
segments.length.should.equal(0);
});
});