mirror of
https://github.com/LightAir/turbo-rss.git
synced 2026-02-04 20:16:19 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ec44f8e5d3 | ||
|
|
7770e925fd | ||
|
|
9864d6408a | ||
|
|
3d5258a6c7 | ||
|
|
a8e4ba436c | ||
|
|
acd60d5922 | ||
| cfea55ea4c | |||
|
|
93815ec708 | ||
|
|
c5a7ff6e1c | ||
|
|
2d3f75902f |
@@ -13,9 +13,4 @@ after_script:
|
||||
- codeclimate-test-reporter < ./coverage/lcov.info
|
||||
matrix:
|
||||
include:
|
||||
- node_js: '8.11.2'
|
||||
before_install: npm -g i npm@2
|
||||
- node_js: '6.11.2'
|
||||
before_install: npm -g i npm@2
|
||||
- node_js: '10.2.1'
|
||||
before_install: npm -g i npm@2
|
||||
- node_js: 'lts/*'
|
||||
|
||||
94
lib/index.js
94
lib/index.js
@@ -4,11 +4,12 @@ const xml = require('xml');
|
||||
|
||||
/**
|
||||
* Check first argument. If true - push last argument to second argument
|
||||
*
|
||||
* @param condition
|
||||
* @param array
|
||||
* @param data
|
||||
*/
|
||||
function ifTruePush(condition, array, data) {
|
||||
function pushIfConditionTrue(condition, array, data) {
|
||||
if (condition) {
|
||||
array.push(data);
|
||||
}
|
||||
@@ -17,9 +18,9 @@ function ifTruePush(condition, array, data) {
|
||||
/**
|
||||
* @param related
|
||||
* @param itemValues
|
||||
* @param relatedfinity
|
||||
* @param relatedInfinity
|
||||
*/
|
||||
function addRelated(related, itemValues, relatedfinity) {
|
||||
function addRelated(related, itemValues, relatedInfinity) {
|
||||
let relatedResult = related.map(function (rel) {
|
||||
return {
|
||||
link: [{
|
||||
@@ -31,13 +32,32 @@ function addRelated(related, itemValues, relatedfinity) {
|
||||
};
|
||||
});
|
||||
|
||||
if (relatedfinity) {
|
||||
if (relatedInfinity) {
|
||||
relatedResult.push({
|
||||
_attr: {'type': 'infinity'}
|
||||
});
|
||||
}
|
||||
|
||||
ifTruePush(related, itemValues, {'yandex:related': relatedResult});
|
||||
pushIfConditionTrue(related, itemValues, {'yandex:related': relatedResult});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param item
|
||||
* @param itemValues
|
||||
*/
|
||||
function pushGoals(item, itemValues) {
|
||||
if (item.goals.length > 0) {
|
||||
item.goals.forEach(goal => itemValues.push({
|
||||
'turbo:goal': {
|
||||
_attr: {
|
||||
type: goal.type || 'yandex',
|
||||
'turbo-goal-id': goal.id,
|
||||
name: goal.name,
|
||||
id: goal.counter_id,
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,17 +67,16 @@ function addRelated(related, itemValues, relatedfinity) {
|
||||
*/
|
||||
function items(items, channel) {
|
||||
items.forEach(function (item) {
|
||||
let item_values = [];
|
||||
item_values.push({_attr: {'turbo': 'true'}});
|
||||
item_values.push({link: item.url});
|
||||
let itemValues = [];
|
||||
itemValues.push({_attr: {'turbo': item.turboEnabled ? 'true' : 'false'}});
|
||||
itemValues.push({link: 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});
|
||||
pushIfConditionTrue(item.turboSource, itemValues, {'turbo:source': item.turboSource});
|
||||
pushIfConditionTrue(item.turboTopic, itemValues, {'turbo:topic': item.turboTopic});
|
||||
pushIfConditionTrue(item.date, itemValues, {pubDate: new Date(item.date).toUTCString()});
|
||||
pushIfConditionTrue(item.author, itemValues, {author: item.author});
|
||||
|
||||
let img = '';
|
||||
let menu = '';
|
||||
let img = '', menu = '';
|
||||
|
||||
if (item.image_url) {
|
||||
img = '<figure><img src="' + item.image_url + '" /></figure>';
|
||||
@@ -71,13 +90,15 @@ function items(items, channel) {
|
||||
|
||||
let fullContent = '<header>' + img + ' <h1>' + item.title + '</h1>' + menu + '</header>' + item.content;
|
||||
|
||||
item_values.push({'turbo:content': {_cdata: fullContent}});
|
||||
pushGoals(item, itemValues);
|
||||
|
||||
itemValues.push({'turbo:content': {_cdata: fullContent}});
|
||||
|
||||
if (typeof item.related !== 'undefined') {
|
||||
addRelated(item.related, item_values, item.relatedfinity);
|
||||
addRelated(item.related, itemValues, item.relatedfinity);
|
||||
}
|
||||
|
||||
channel.push({item: item_values});
|
||||
channel.push({item: itemValues});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -111,6 +132,28 @@ function generateXML(data) {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param data
|
||||
* @returns {*}
|
||||
*/
|
||||
function itemData(data) {
|
||||
return {
|
||||
title: data.title || '',
|
||||
description: data.description || '',
|
||||
image_url: data.image_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 || '',
|
||||
goals: data.goals || [],
|
||||
turboEnabled: data.turboEnabled !== undefined ? data.turboEnabled: true,
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Base function
|
||||
* @param options
|
||||
@@ -129,22 +172,7 @@ function TR(options, items) {
|
||||
|
||||
this.item = function (data) {
|
||||
data = data || {};
|
||||
let item = {
|
||||
title: data.title || '',
|
||||
description: data.description || '',
|
||||
image_url: data.image_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);
|
||||
this.items.push(itemData(data));
|
||||
return this;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "turbo-rss",
|
||||
"version": "1.0.8",
|
||||
"version": "1.1.1",
|
||||
"description": "RSS based, feed generator for Yandex turbo",
|
||||
"keywords": [
|
||||
"yandex",
|
||||
|
||||
17
readme.md
17
readme.md
@@ -45,7 +45,8 @@ feed.item(itemOptions);
|
||||
* `relatedfinity` _optional_ **bool** Непрерывная лента статей
|
||||
* `turboSource` _optional_ **string** URL страницы-источника, который можно передать в Яндекс.Метрику.
|
||||
* `turboTopic` _optional_ **string** Заголовок страницы, который можно передать в Яндекс.Метрику.
|
||||
|
||||
* `goals` _optional_ **array** массив типа: { _id_ - внутренний идентификатор цели (turbo-goal-id), _name_ - имя цели, _counter_id_ - id счётчика яндекс-метрики }
|
||||
* `turboEnabled`_optional_ **bool** Принудительная установка атрибута "turbo". По умолчанию true. Установка в false позволит скрыть отображение турбо-страницы
|
||||
###### menu array
|
||||
menu должен содержать массив объектов со следующими опциями:
|
||||
|
||||
@@ -84,6 +85,12 @@ feed.item({
|
||||
author: 'LightAir',
|
||||
date: 'May 27, 2012',
|
||||
content: '<p>hello</p>',
|
||||
goals: [{
|
||||
type: "yandex",
|
||||
id: "turbo-goal-id",
|
||||
counter_id: "12345",
|
||||
name: "order",
|
||||
}],
|
||||
menu: [{
|
||||
link: 'http://example.com/',
|
||||
text: 'Главная'
|
||||
@@ -112,3 +119,11 @@ var xml = feed.xml();
|
||||
```sh
|
||||
$ npm test
|
||||
```
|
||||
|
||||
## Спасибо
|
||||
|
||||
@jahglow
|
||||
|
||||
@vvmspace
|
||||
|
||||
@crackosok
|
||||
1
test/expectedOutput/defaultItemTurboFalse.xml
Normal file
1
test/expectedOutput/defaultItemTurboFalse.xml
Normal file
@@ -0,0 +1 @@
|
||||
<rss xmlns:yandex="http://news.yandex.ru" xmlns:media="http://search.yahoo.com/mrss/" xmlns:turbo="http://turbo.yandex.ru" version="2.0"><channel><title><![CDATA[title]]></title><link>http://example.com/rss.xml</link><description><![CDATA[description]]></description><language>ru</language><item turbo="false"><link></link><turbo:content><![CDATA[<header> <h1></h1></header>undefined]]></turbo:content></item></channel></rss>
|
||||
1
test/expectedOutput/goal.xml
Normal file
1
test/expectedOutput/goal.xml
Normal file
@@ -0,0 +1 @@
|
||||
<rss xmlns:yandex="http://news.yandex.ru" xmlns:media="http://search.yahoo.com/mrss/" xmlns:turbo="http://turbo.yandex.ru" version="2.0"><channel><title><![CDATA[title]]></title><link>http://example.com/rss.xml</link><description><![CDATA[description]]></description><language>ru</language><item turbo="true"><link>http://example.com/article4?this&that</link><pubDate>Sat, 26 May 2018 21:00:00 GMT</pubDate><author>vvm.space</author><turbo:goal type="yandex" turbo-goal-id="turbo-goal-id" name="order" id="12345"/><turbo:content><![CDATA[<header><figure><img src="http://example.com/example.png" /></figure> <h1>item title</h1></header><p>hello</p>]]></turbo:content><yandex:related><link url="http://example.com/related/post1" img="http://example.com/i/img1.jpg">related link text 1</link><link url="http://example.com/related/post2" img="http://example.com/i/img2.jpg">related link text 2</link></yandex:related></item></channel></rss>
|
||||
@@ -1,7 +1,7 @@
|
||||
// prova is a wrapper for tape
|
||||
// use npm run test:browser to run tests in a browser
|
||||
/*
|
||||
* use npm test to run tests
|
||||
*/
|
||||
const test = require('tape');
|
||||
|
||||
const TR = require('..');
|
||||
|
||||
const includeFolder = require('include-folder');
|
||||
@@ -31,6 +31,22 @@ test('default item', function (t) {
|
||||
t.equal(feed.xml(), expectedOutput.defaultItem.trim());
|
||||
});
|
||||
|
||||
test('default item turbo false', 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({
|
||||
turboEnabled: false
|
||||
});
|
||||
|
||||
t.equal(feed.xml(), expectedOutput.defaultItemTurboFalse.trim());
|
||||
});
|
||||
|
||||
test('related item', function (t) {
|
||||
t.plan(1);
|
||||
let feed = new TR({
|
||||
@@ -132,3 +148,40 @@ test('menu', function (t) {
|
||||
|
||||
t.equal(feed.xml(), expectedOutput.menu.trim());
|
||||
});
|
||||
|
||||
test('goals', 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: 'vvm.space',
|
||||
date: 'May 27, 2018 00:00 AM',
|
||||
menu: '<a href="http://example.com/page1.html">Текст ссылки</a> <a href="http://example.com/page2.html">Текст ссылки</a>',
|
||||
goals: [{
|
||||
type: "yandex",
|
||||
id: "turbo-goal-id",
|
||||
counter_id: "12345",
|
||||
name: "order",
|
||||
}],
|
||||
content: '<p>hello</p>',
|
||||
related: [{
|
||||
link: 'http://example.com/related/post1',
|
||||
image_url: 'http://example.com/i/img1.jpg',
|
||||
text: 'related link text 1'
|
||||
}, {
|
||||
link: 'http://example.com/related/post2',
|
||||
image_url: 'http://example.com/i/img2.jpg',
|
||||
text: 'related link text 2'
|
||||
}]
|
||||
});
|
||||
|
||||
t.equal(feed.xml(), expectedOutput.goal.trim());
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user