diff --git a/lib/index.js b/lib/index.js
index c178e46..0434ce5 100755
--- a/lib/index.js
+++ b/lib/index.js
@@ -50,8 +50,9 @@ function items(items, channel) {
let item_values = [];
item_values.push({_attr: {'turbo': 'true'}});
item_values.push({link: item.url});
- item_values.push({'turbo:source': item.url});
+ ifTruePush(item.turboSource, item_values, {'turbo:source': item.turboSource});
+ ifTruePush(item.turboTopic, item_values, {'turbo:topic': item.turboTopic});
ifTruePush(item.date, item_values, {pubDate: new Date(item.date).toUTCString()});
ifTruePush(item.author, item_values, {author: item.author});
@@ -62,8 +63,10 @@ function items(items, channel) {
img = '';
}
- if (item.menu) {
- menu = '
';
+ if (Array.isArray(item.menu)) {
+ menu = '';
}
let fullContent = '' + img + '
' + item.title + '
' + menu + '' + item.content;
@@ -87,9 +90,9 @@ function generateXML(data) {
let channel = [];
channel.push({title: {_cdata: data.title}});
- channel.push({link: data.link || 'http://github.com/LightAir/turbo-rss'});
+ channel.push({link: data.link});
channel.push({description: {_cdata: data.description || data.title}});
- channel.push({language: 'ru'});
+ channel.push({language: data.language});
items(data.items, channel);
@@ -114,27 +117,31 @@ function generateXML(data) {
* @param items
* @constructor
*/
-function YTurbo(options, items) {
+function TR(options, items) {
options = options || {};
- this.title = options.title || 'Untitled';
+ this.title = options.title || '';
+ this.link = options.link || '';
this.description = options.description || '';
- this.link = options.link;
+ this.language = options.language || 'ru';
+
this.items = items || [];
this.item = function (data) {
data = data || {};
let item = {
- title: data.title || 'No title',
+ title: data.title || '',
description: data.description || '',
image_url: data.image_url,
- url: data.url,
+ url: data.url || data.link,
author: data.author,
date: data.date || data.pubDate,
content: data.content,
menu: data.menu,
related: data.related,
relatedfinity: data.relatedfinity || false,
+ turboSource: data.turboSource || '',
+ turboTopic: data.turboTopic || ''
};
this.items.push(item);
@@ -146,4 +153,4 @@ function YTurbo(options, items) {
};
}
-module.exports = YTurbo;
+module.exports = TR;
diff --git a/package.json b/package.json
index 652c1fa..738a556 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "turbo-rss",
- "version": "1.0.7",
+ "version": "1.0.8",
"description": "RSS based, feed generator for Yandex turbo",
"keywords": [
"yandex",
@@ -20,6 +20,7 @@
},
"contributors": [
"LightAir ",
+ "Ivan Pilyugin (aka Archer)",
"Dylan Greene ",
"Xavier Damman ",
"Michael R. Lange",
diff --git a/readme.md b/readme.md
index 2d63f1d..a139187 100644
--- a/readme.md
+++ b/readme.md
@@ -1,5 +1,3 @@
-## WIP
-
## turbo-rss
[](https://codeclimate.com/github/LightAir/turbo-rss/maintainability)
@@ -8,9 +6,7 @@
[](https://www.npmjs.com/package/turbo-rss)

->Генератор RSS разметки для сервиса Турбо-страницы
-
->ПРЕДУПРЕЖДЕНИЕ! Работа в процессе
+>Генератор RSS разметки для сервиса Турбо-страницы https://yandex.ru/support/webmaster/turbo/connection.html
### Использование
@@ -25,12 +21,11 @@ var feed = new TR(feedOptions);
##### `Опции канала`
* `title` **string** Название RSS-канала.
- * `description` _optional_ **string** Описание канала одним предложением. Не используйте HTML-разметку..
- * `author` _optional_ **string** If included it is the name of the item's creator. **(Будет удалено)**
- * `link` **url string** Домен сайта, данные которого транслируются..
- * `pubDate` _optional_ **Date object or date string** The publication date for content in the feed **(Будет удалено)**
+ * `link` **url string** Домен сайта, данные которого транслируются.
+ * `description` _optional_ **string** Описание канала одним предложением. Не используйте HTML-разметку.
+ * `language` _optional_ **string** Язык статьи по стандарту ISO 639-1. По умолчанию ru.
- *Будет добавлено в новых версиях turbo:analytics, turbo:adNetwork*
+ *Будет добавлено в новых версиях turbo:analytics, turbo:adNetwork, на текущий момент можно добавить в интерфейсе Яндекс Вебмастер*
#### Добавление страницы в канал
```js
@@ -40,22 +35,29 @@ feed.item(itemOptions);
##### itemOptions
* `title` **string** Заголовок страницы.
- * `image_url` **url string** Адрес изображения, которое используется в качестве обложки. Изображение может быть в любом формате.
- * `url` **url string** URL страницы сайта, для которой нужно сформировать Турбо-страницу.
+ * `image_url` _optional_ **url string** Адрес изображения, которое используется в качестве обложки. Изображение может быть в любом формате.
+ * `link` **url string** URL страницы сайта, для которой нужно сформировать Турбо-страницу.
* `author` _optional_ **string** Автор статьи, размещенной на странице.
- * `date` **string** Время публикации контента на сайте источника.
+ * `date` или `pubDate` **string** Время публикации контента на сайте источника. Передается в формате RFC-822.
* `content` **string** Содержимое страницы
- * `related` _optional_ **array** Аффилированные ссылки `yandex:related` в конце статьи.
+ * `menu` _optional_ **array** Внимание! Меню будет отображаться только в том случае, если в настройках на странице Яндекс Вебмастер -> Турбо-страницы -> Настройки, содержимое 'Меню Турбо-страниц' пустое!
+ * `related` _optional_ **array** Аффилированные ссылки `yandex:related` в конце статьи. Вы можете разместить ссылки на другие ресурсы или настроить отображение непрерывной ленты статей, реализованной, например с помощью AJAX.
* `relatedfinity` _optional_ **bool** Непрерывная лента статей
-
- *Будет добавлено в новых версиях turbo:source, turbo:topic, menu, pubDate как алиас date*
+ * `turboSource` _optional_ **string** URL страницы-источника, который можно передать в Яндекс.Метрику.
+ * `turboTopic` _optional_ **string** Заголовок страницы, который можно передать в Яндекс.Метрику.
+
+###### menu array
+ menu должен содержать массив объектов со следующими опциями:
+
+ * `link` **url string** ссылка
+ * `text` **string** текст ссылки. не должен содержать html
###### related array
related должен содержать массив объектов со следующими опциями:
- * `link` **string** ссылка на статью'
- * `image_url` **string** ссылка на изображение к статье
- * `text` **string** текст ссылки
+ * `link` **url string** ссылка на статью
+ * `image_url` **url string** ссылка на изображение к статье
+ * `text` **string** текст ссылки. Не должен содержать html
##### Получение XML
@@ -69,14 +71,12 @@ var xml = feed.xml();
```js
var TR = require('turbo-rss');
-/* lets create feed */
var feed = new TR({
title: 'title',
description: 'description',
link: 'http://example.com',
});
-/* loop over data and add to feed */
feed.item({
title: 'item title',
image_url: 'http://example.com/example.png',
@@ -84,6 +84,13 @@ feed.item({
author: 'LightAir',
date: 'May 27, 2012',
content: '
hello
',
+ menu: [{
+ link: 'http://example.com/',
+ text: 'Главная'
+ }, {
+ link: 'http://example.com/about',
+ text: 'О сайте'
+ }]
related: [{
link: 'http://example.com/related/post1',
image_url: 'http://example.com/i/img1.jpg',
@@ -95,13 +102,12 @@ feed.item({
}]
});
-// cache the xml to send to clients
var xml = feed.xml();
```
## Тестирование
-Для запуска тестов выполните `npm test`. На текущий момен покрытие тестами не 100%
+Для запуска тестов выполните `npm test`.
```sh
$ npm test
diff --git a/test/expectedOutput/default.xml b/test/expectedOutput/default.xml
index ca484e0..41ecb51 100644
--- a/test/expectedOutput/default.xml
+++ b/test/expectedOutput/default.xml
@@ -1 +1 @@
-http://github.com/LightAir/turbo-rssru
+ru
diff --git a/test/expectedOutput/defaultItem.xml b/test/expectedOutput/defaultItem.xml
index 35555d1..48321b5 100644
--- a/test/expectedOutput/defaultItem.xml
+++ b/test/expectedOutput/defaultItem.xml
@@ -1 +1 @@
-http://example.com/rss.xmlru
undefined]]>
+ru undefined]]>
diff --git a/test/expectedOutput/menu.xml b/test/expectedOutput/menu.xml
new file mode 100644
index 0000000..afba1b6
--- /dev/null
+++ b/test/expectedOutput/menu.xml
@@ -0,0 +1 @@
+http://example.com/rss.xmlruhttp://example.com/article4?this&thatSat, 26 May 2018 21:00:00 GMTLightAir
item title
hello
]]>related link text 1related link text 2 undefined]]>
diff --git a/test/expectedOutput/relatedItem.xml b/test/expectedOutput/relatedItem.xml
index e083e44..7d386f7 100644
--- a/test/expectedOutput/relatedItem.xml
+++ b/test/expectedOutput/relatedItem.xml
@@ -1 +1 @@
-http://example.com/rss.xmlruhttp://example.com/article4?this&thathttp://example.com/article4?this&thatSat, 26 May 2018 21:00:00 GMTLightAir
item title
hello
]]>related link text 1related link text 2
+http://example.com/rss.xmlruhttp://example.com/article4?this&thatSat, 26 May 2018 21:00:00 GMTLightAir
item title
hello
]]>related link text 1related link text 2
diff --git a/test/expectedOutput/relatedItemInfinity.xml b/test/expectedOutput/relatedItemInfinity.xml
index 1139f43..bd80cb4 100644
--- a/test/expectedOutput/relatedItemInfinity.xml
+++ b/test/expectedOutput/relatedItemInfinity.xml
@@ -1 +1 @@
-http://example.com/rss.xmlruhttp://example.com/article4?this&thathttp://example.com/article4?this&thatSat, 26 May 2018 21:00:00 GMTLightAir
item title
hello
]]>related link text 1related link text 2
+http://example.com/rss.xmlruhttp://example.com/article4?this&thatSat, 26 May 2018 21:00:00 GMTLightAir
item title
hello
]]>related link text 1related link text 2
diff --git a/test/index.js b/test/index.js
index c6a3399..22456f9 100644
--- a/test/index.js
+++ b/test/index.js
@@ -2,24 +2,24 @@
// use npm run test:browser to run tests in a browser
const test = require('tape');
-const YTurbo = require('..');
+const TR = require('..');
const includeFolder = require('include-folder');
const expectedOutput = includeFolder(__dirname + '/expectedOutput', /.*\.xml$/);
require('mockdate').set('Wed, 10 Dec 2014 19:04:57 GMT');
-test('empty feed', function(t) {
+test('empty feed', function (t) {
t.plan(2);
- let feed = new YTurbo();
+ let feed = new TR();
t.equal(feed.xml(), expectedOutput.default.trim());
feed.item();
t.equal(feed.xml(), expectedOutput.defaultOneItem.trim());
});
-test('default item', function(t) {
+test('default item', function (t) {
t.plan(1);
- let feed = new YTurbo({
+ let feed = new TR({
title: 'title',
description: 'description',
link: 'http://example.com/rss.xml',
@@ -31,9 +31,9 @@ test('default item', function(t) {
t.equal(feed.xml(), expectedOutput.defaultItem.trim());
});
-test('related item', function(t) {
+test('related item', function (t) {
t.plan(1);
- let feed = new YTurbo({
+ let feed = new TR({
title: 'title',
description: 'description',
link: 'http://example.com/rss.xml',
@@ -41,7 +41,7 @@ test('related item', function(t) {
});
feed.item({
- title: 'item title',
+ title: 'item title',
image_url: 'http://example.com/example.png',
url: 'http://example.com/article4?this&that',
author: 'LightAir',
@@ -62,9 +62,9 @@ test('related item', function(t) {
t.equal(feed.xml(), expectedOutput.relatedItem.trim());
});
-test('related item', function(t) {
+test('related item', function (t) {
t.plan(1);
- let feed = new YTurbo({
+ let feed = new TR({
title: 'title',
description: 'description',
link: 'http://example.com/rss.xml',
@@ -72,7 +72,7 @@ test('related item', function(t) {
});
feed.item({
- title: 'item title',
+ title: 'item title',
image_url: 'http://example.com/example.png',
url: 'http://example.com/article4?this&that',
author: 'LightAir',
@@ -91,4 +91,44 @@ test('related item', function(t) {
});
t.equal(feed.xml(), expectedOutput.relatedItemInfinity.trim());
-});
\ No newline at end of file
+});
+
+test('menu', function (t) {
+ t.plan(1);
+ let feed = new TR({
+ title: 'title',
+ description: 'description',
+ link: 'http://example.com/rss.xml',
+ site_url: 'http://example.com'
+ });
+
+ feed.item({
+ title: 'item title',
+ image_url: 'http://example.com/example.png',
+ url: 'http://example.com/article4?this&that',
+ author: 'LightAir',
+ date: 'May 27, 2018 00:00 AM',
+ content: '