Skip to content

Commit

Permalink
add title template (garmeeh#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
erickeno authored and garmeeh committed Aug 27, 2018
1 parent 7aec9a6 commit e2b7281
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/meta/__tests__/buildTags.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,18 @@ it('correctly sets noindex, nofollow', () => {
expect(Array.from(index).length).toBe(0);
expect(Array.from(noindex).length).toBe(2);
});

it('displays title with titleTemplate integrated', () => {
const template = 'Next SEO';
const overrideProps = {
...SEO,
titleTemplate: `${template} | %s`,
};
const tags = buildTags(overrideProps);
const { container } = render(tags);
const title = getByText(
container,
(content, element) => element.tagName.toLowerCase() === 'title' && content.startsWith(template),
);
expect(title.innerHTML).toMatch(`${template} | ${SEO.title}`);
});
6 changes: 5 additions & 1 deletion src/meta/buildTags.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const buildTags = (config) => {
const tagsToRender = [];

if (config.title) {
tagsToRender.push(<title key="title">{config.title}</title>);
let updatedTitle = config.title;
if (config.titleTemplate) {
updatedTitle = config.titleTemplate.replace(/%s/g, () => updatedTitle);
}
tagsToRender.push(<title key="title">{updatedTitle}</title>);
}

if (!config.noindex) {
Expand Down

0 comments on commit e2b7281

Please sign in to comment.