Skip to content

Commit

Permalink
Merge pull request #342 from ekalinin/better-hreflang-support
Browse files Browse the repository at this point in the history
investigated #335, #340
  • Loading branch information
derduher authored Dec 23, 2020
2 parents 2a9c7ad + 6914972 commit f6fc9fc
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 10 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Changelog

## unreleased
## 6.3.4

- bump dependencies
- correct return type of xmllint. Was Promise<null> but actually returned Promise<void>
- correct return type of xmllint. Was `Promise<null>` but actually returned `Promise<void>`
- add alternate option for lang, hreflang as that is the actual name of the printed attribute

## 6.3.3

Expand Down
17 changes: 14 additions & 3 deletions examples/sitemapAndIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ const sms = new SitemapAndIndexStream({
// for it to write the sitemap urls to and the expected url where that sitemap will be hosted
getSitemapStream: (i) => {
const sitemapStream = new SitemapStream({
hostname: 'https://example.com',
hostname: 'https://example.ru/',
});
const path = `./sitemap-${i}.xml`;

const ws = createWriteStream(resolve(path + '.gz'));
sitemapStream
.pipe(createGzip()) // compress the output of the sitemap
.pipe(createWriteStream(resolve(path + '.gz'))); // write it to sitemap-NUMBER.xml
.pipe(ws); // write it to sitemap-NUMBER.xml

return [
new URL(path, 'https://example.com/subdir/').toString(),
sitemapStream,
ws,
];
},
});
Expand All @@ -40,6 +42,15 @@ sms
.pipe(createGzip())
.pipe(createWriteStream(resolve('./sitemap-index.xml.gz')));

const arrayOfSitemapItems = [{ url: '/page-1/', changefreq: 'daily' }];
const arrayOfSitemapItems = [
{ url: '/page-1/', changefreq: 'daily' },
{
url: '/docs',
links: [
{ lang: 'ru', url: 'https://example.ru/docs' },
{ lang: 'en', url: 'https://example.com/docs' },
],
},
];
arrayOfSitemapItems.forEach((item) => sms.write(item));
sms.end();
2 changes: 1 addition & 1 deletion lib/sitemap-item-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export class SitemapItemStream extends Transform {
this.push(
element(TagNames['xhtml:link'], {
rel: 'alternate',
hreflang: link.lang,
hreflang: link.lang || link.hreflang,
href: link.url,
})
);
Expand Down
4 changes: 4 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ export interface LinkItem {
* @example 'en'
*/
lang: string;
/**
* @example 'en-us'
*/
hreflang?: string;
url: string;
}

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sitemap",
"version": "6.3.3",
"version": "6.3.4",
"description": "Sitemap-generating lib/cli",
"keywords": [
"sitemap",
Expand Down
34 changes: 32 additions & 2 deletions tests/sitemap-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ErrorLevel,
SitemapItemLoose,
EnumChangefreq,
SitemapStream,
} from '../index';
import * as testUtil from './util';
import {
Expand All @@ -13,6 +14,7 @@ import {
normalizeURL,
} from '../lib/utils';
import { Readable, Writable } from 'stream';
import { streamToPromise } from '../lib/sitemap-stream';

describe('utils', () => {
let itemTemplate: SitemapItem;
Expand Down Expand Up @@ -693,7 +695,7 @@ describe('utils', () => {
});

it('turns a line-separated stream into a sitemap', async () => {
await new Promise((resolve) => {
await new Promise<void>((resolve) => {
lineSeparatedURLsToSitemapOptions(rs).pipe(ws);
ws.on('finish', () => resolve());
});
Expand All @@ -704,7 +706,7 @@ describe('utils', () => {

it('turns a line-separated JSON stream into a sitemap', async () => {
let osampleURLs: { url: string }[];
await new Promise((resolve) => {
await new Promise<void>((resolve) => {
osampleURLs = sampleURLs.map((url) => ({ url }));
content = osampleURLs.map((url) => JSON.stringify(url)).join('\n');
lineSeparatedURLsToSitemapOptions(rs, { isJSON: true }).pipe(ws);
Expand Down Expand Up @@ -818,6 +820,34 @@ describe('utils', () => {
);
});

it('does not prepend provided hostname to links that already have a hostname', async () => {
const sms = new SitemapStream({ hostname: 'https://example.ru/' });
sms.write({
url: '/docs',
links: [
{ lang: 'ru', url: 'https://example.ru/docs' },
{ lang: 'en', url: 'https://example.com/docs' },
],
});
sms.end();

expect((await streamToPromise(sms)).toString()).toContain(
'https://example.com/docs'
);

const url = {
url: 'http://example.ru',
links: [
{ url: 'http://example.com/lang', lang: 'en-us' },
{ url: '/lang', lang: 'en-us' },
],
};
expect(normalizeURL(url, 'http://example.ru').links[0]).toHaveProperty(
'url',
'http://example.com/lang'
);
});

describe('video', () => {
it('is ensured to be an array', () => {
expect(
Expand Down

0 comments on commit f6fc9fc

Please sign in to comment.