mirror of
https://github.com/LightAir/turbo-rss.git
synced 2026-02-04 20:16:19 +00:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9cccd65768 | ||
|
|
812a429d9b | ||
|
|
15c9715cee | ||
|
|
816b6b5690 | ||
|
|
ef90f974bd | ||
|
|
8aa086865d | ||
|
|
6d3cd71f47 | ||
|
|
ec44f8e5d3 | ||
|
|
7770e925fd | ||
|
|
9864d6408a | ||
|
|
3d5258a6c7 | ||
|
|
a8e4ba436c | ||
|
|
acd60d5922 |
@@ -13,9 +13,4 @@ after_script:
|
|||||||
- codeclimate-test-reporter < ./coverage/lcov.info
|
- codeclimate-test-reporter < ./coverage/lcov.info
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
- node_js: '8.11.2'
|
- node_js: 'lts/*'
|
||||||
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
|
|
||||||
|
|||||||
67
lib/index.js
67
lib/index.js
@@ -4,11 +4,12 @@ const xml = require('xml');
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Check first argument. If true - push last argument to second argument
|
* Check first argument. If true - push last argument to second argument
|
||||||
|
*
|
||||||
* @param condition
|
* @param condition
|
||||||
* @param array
|
* @param array
|
||||||
* @param data
|
* @param data
|
||||||
*/
|
*/
|
||||||
function ifTruePush(condition, array, data) {
|
function pushIfConditionTrue(condition, array, data) {
|
||||||
if (condition) {
|
if (condition) {
|
||||||
array.push(data);
|
array.push(data);
|
||||||
}
|
}
|
||||||
@@ -17,9 +18,9 @@ function ifTruePush(condition, array, data) {
|
|||||||
/**
|
/**
|
||||||
* @param related
|
* @param related
|
||||||
* @param itemValues
|
* @param itemValues
|
||||||
* @param relatedfinity
|
* @param relatedInfinity
|
||||||
*/
|
*/
|
||||||
function addRelated(related, itemValues, relatedfinity) {
|
function addRelated(related, itemValues, relatedInfinity) {
|
||||||
let relatedResult = related.map(function (rel) {
|
let relatedResult = related.map(function (rel) {
|
||||||
return {
|
return {
|
||||||
link: [{
|
link: [{
|
||||||
@@ -31,13 +32,32 @@ function addRelated(related, itemValues, relatedfinity) {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
if (relatedfinity) {
|
if (relatedInfinity) {
|
||||||
relatedResult.push({
|
relatedResult.push({
|
||||||
_attr: {'type': 'infinity'}
|
_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) {
|
function items(items, channel) {
|
||||||
items.forEach(function (item) {
|
items.forEach(function (item) {
|
||||||
let item_values = [];
|
let itemValues = [];
|
||||||
item_values.push({_attr: {'turbo': 'true'}});
|
itemValues.push({_attr: {'turbo': item.turboEnabled ? 'true' : 'false'}});
|
||||||
item_values.push({link: item.url});
|
itemValues.push({link: item.url});
|
||||||
|
|
||||||
ifTruePush(item.turboSource, item_values, {'turbo:source': item.turboSource});
|
pushIfConditionTrue(item.turboSource, itemValues, {'turbo:source': item.turboSource});
|
||||||
ifTruePush(item.turboTopic, item_values, {'turbo:topic': item.turboTopic});
|
pushIfConditionTrue(item.turboTopic, itemValues, {'turbo:topic': item.turboTopic});
|
||||||
ifTruePush(item.date, item_values, {pubDate: new Date(item.date).toUTCString()});
|
pushIfConditionTrue(item.date, itemValues, {pubDate: new Date(item.date).toUTCString()});
|
||||||
ifTruePush(item.author, item_values, {author: item.author});
|
pushIfConditionTrue(item.author, itemValues, {author: item.author});
|
||||||
|
|
||||||
let img = '';
|
let img = '', menu = '';
|
||||||
let menu = '';
|
|
||||||
|
|
||||||
if (item.image_url) {
|
if (item.image_url) {
|
||||||
img = '<figure><img src="' + item.image_url + '" /></figure>';
|
img = '<figure><img src="' + item.image_url + '" /></figure>';
|
||||||
@@ -71,26 +90,15 @@ function items(items, channel) {
|
|||||||
|
|
||||||
let fullContent = '<header>' + img + ' <h1>' + item.title + '</h1>' + menu + '</header>' + item.content;
|
let fullContent = '<header>' + img + ' <h1>' + item.title + '</h1>' + menu + '</header>' + item.content;
|
||||||
|
|
||||||
if (item.goals.length > 0) {
|
pushGoals(item, itemValues);
|
||||||
item.goals.forEach(goal => item_values.push({
|
|
||||||
"turbo:goal": {
|
|
||||||
_attr: {
|
|
||||||
type: goal.type || 'yandex',
|
|
||||||
'turbo-goal-id': goal.id,
|
|
||||||
name: goal.name,
|
|
||||||
id: goal.counter_id,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
item_values.push({'turbo:content': {_cdata: fullContent}});
|
itemValues.push({'turbo:content': {_cdata: fullContent}});
|
||||||
|
|
||||||
if (typeof item.related !== 'undefined') {
|
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});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,6 +151,7 @@ function itemData(data) {
|
|||||||
turboSource: data.turboSource || '',
|
turboSource: data.turboSource || '',
|
||||||
turboTopic: data.turboTopic || '',
|
turboTopic: data.turboTopic || '',
|
||||||
goals: data.goals || [],
|
goals: data.goals || [],
|
||||||
|
turboEnabled: data.turboEnabled !== undefined ? data.turboEnabled: true,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|||||||
30
package.json
30
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "turbo-rss",
|
"name": "turbo-rss",
|
||||||
"version": "1.1.0",
|
"version": "1.2.2",
|
||||||
"description": "RSS based, feed generator for Yandex turbo",
|
"description": "RSS based, feed generator for Yandex turbo",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"yandex",
|
"yandex",
|
||||||
@@ -10,8 +10,7 @@
|
|||||||
],
|
],
|
||||||
"main": "lib/index",
|
"main": "lib/index",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "tape test --tap | tap-difflet",
|
"test": "tape test --tap | tap-min"
|
||||||
"coverage": "istanbul cover tape test -- -R spec"
|
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/LightAir/turbo-rss",
|
"homepage": "https://github.com/LightAir/turbo-rss",
|
||||||
"author": {
|
"author": {
|
||||||
@@ -56,27 +55,12 @@
|
|||||||
"xml": "1.0.1"
|
"xml": "1.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^4.19.1",
|
"eslint": "^7.28.0",
|
||||||
"folderify": "^1.1.0",
|
|
||||||
"grunt": "^0.4.5",
|
|
||||||
"grunt-cli": "^0.1.13",
|
|
||||||
"grunt-contrib-jshint": "^0.11.3",
|
|
||||||
"grunt-release": "^0.13.0",
|
|
||||||
"grunt-templates-dylang": "^1.0.10",
|
|
||||||
"include-folder": "^1.0.0",
|
"include-folder": "^1.0.0",
|
||||||
"load-grunt-tasks": "^3.3.0",
|
"mockdate": "^3.0.5",
|
||||||
"mockdate": "^1.0.3",
|
"q": "^1.5.1",
|
||||||
"prova": "^2.1.2",
|
"tap-min": "~2.0.0",
|
||||||
"q": "^1.4.1",
|
"tape": "^5.2.2"
|
||||||
"tap-difflet": "^0.4.0",
|
|
||||||
"tape": "^4.2.1",
|
|
||||||
"time-grunt": "^1.2.1",
|
|
||||||
"xml2js": "^0.4.12"
|
|
||||||
},
|
|
||||||
"browserify": {
|
|
||||||
"transform": [
|
|
||||||
"folderify"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
}
|
}
|
||||||
|
|||||||
12
readme.md
12
readme.md
@@ -46,7 +46,7 @@ feed.item(itemOptions);
|
|||||||
* `turboSource` _optional_ **string** URL страницы-источника, который можно передать в Яндекс.Метрику.
|
* `turboSource` _optional_ **string** URL страницы-источника, который можно передать в Яндекс.Метрику.
|
||||||
* `turboTopic` _optional_ **string** Заголовок страницы, который можно передать в Яндекс.Метрику.
|
* `turboTopic` _optional_ **string** Заголовок страницы, который можно передать в Яндекс.Метрику.
|
||||||
* `goals` _optional_ **array** массив типа: { _id_ - внутренний идентификатор цели (turbo-goal-id), _name_ - имя цели, _counter_id_ - id счётчика яндекс-метрики }
|
* `goals` _optional_ **array** массив типа: { _id_ - внутренний идентификатор цели (turbo-goal-id), _name_ - имя цели, _counter_id_ - id счётчика яндекс-метрики }
|
||||||
|
* `turboEnabled`_optional_ **bool** Принудительная установка атрибута "turbo". По умолчанию true. Установка в false позволит скрыть отображение турбо-страницы
|
||||||
###### menu array
|
###### menu array
|
||||||
menu должен содержать массив объектов со следующими опциями:
|
menu должен содержать массив объектов со следующими опциями:
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ feed.item({
|
|||||||
}, {
|
}, {
|
||||||
link: 'http://example.com/about',
|
link: 'http://example.com/about',
|
||||||
text: 'О сайте'
|
text: 'О сайте'
|
||||||
}]
|
}],
|
||||||
related: [{
|
related: [{
|
||||||
link: 'http://example.com/related/post1',
|
link: 'http://example.com/related/post1',
|
||||||
image_url: 'http://example.com/i/img1.jpg',
|
image_url: 'http://example.com/i/img1.jpg',
|
||||||
@@ -119,3 +119,11 @@ var xml = feed.xml();
|
|||||||
```sh
|
```sh
|
||||||
$ npm test
|
$ 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>
|
||||||
103
test/index.js
103
test/index.js
@@ -1,12 +1,26 @@
|
|||||||
// prova is a wrapper for tape
|
|
||||||
// use npm run test:browser to run tests in a browser
|
|
||||||
const test = require('tape');
|
const test = require('tape');
|
||||||
|
|
||||||
const TR = require('..');
|
const TR = require('..');
|
||||||
|
|
||||||
const includeFolder = require('include-folder');
|
const includeFolder = require('include-folder');
|
||||||
const expectedOutput = includeFolder(__dirname + '/expectedOutput', /.*\.xml$/);
|
const expectedOutput = includeFolder(__dirname + '/expectedOutput', /.*\.xml$/);
|
||||||
|
|
||||||
|
const baseOptions = {
|
||||||
|
title: 'title',
|
||||||
|
description: 'description',
|
||||||
|
link: 'http://example.com/rss.xml',
|
||||||
|
site_url: 'http://example.com'
|
||||||
|
};
|
||||||
|
|
||||||
|
const relatedOptions = [{
|
||||||
|
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'
|
||||||
|
}];
|
||||||
|
|
||||||
require('mockdate').set('Wed, 10 Dec 2014 19:04:57 GMT');
|
require('mockdate').set('Wed, 10 Dec 2014 19:04:57 GMT');
|
||||||
|
|
||||||
test('empty feed', function (t) {
|
test('empty feed', function (t) {
|
||||||
@@ -19,12 +33,7 @@ test('empty feed', function (t) {
|
|||||||
|
|
||||||
test('default item', function (t) {
|
test('default item', function (t) {
|
||||||
t.plan(1);
|
t.plan(1);
|
||||||
let feed = new TR({
|
let feed = new TR(baseOptions);
|
||||||
title: 'title',
|
|
||||||
description: 'description',
|
|
||||||
link: 'http://example.com/rss.xml',
|
|
||||||
site_url: 'http://example.com'
|
|
||||||
});
|
|
||||||
|
|
||||||
feed.item({});
|
feed.item({});
|
||||||
|
|
||||||
@@ -33,12 +42,7 @@ test('default item', function (t) {
|
|||||||
|
|
||||||
test('related item', function (t) {
|
test('related item', function (t) {
|
||||||
t.plan(1);
|
t.plan(1);
|
||||||
let feed = new TR({
|
let feed = new TR(baseOptions);
|
||||||
title: 'title',
|
|
||||||
description: 'description',
|
|
||||||
link: 'http://example.com/rss.xml',
|
|
||||||
site_url: 'http://example.com'
|
|
||||||
});
|
|
||||||
|
|
||||||
feed.item({
|
feed.item({
|
||||||
title: 'item title',
|
title: 'item title',
|
||||||
@@ -48,15 +52,7 @@ test('related item', function (t) {
|
|||||||
date: 'May 27, 2018 00:00 AM',
|
date: 'May 27, 2018 00:00 AM',
|
||||||
menu: '<a href="http://example.com/page1.html">Текст ссылки</a> <a href="http://example.com/page2.html">Текст ссылки</a>',
|
menu: '<a href="http://example.com/page1.html">Текст ссылки</a> <a href="http://example.com/page2.html">Текст ссылки</a>',
|
||||||
content: '<p>hello</p>',
|
content: '<p>hello</p>',
|
||||||
related: [{
|
related: relatedOptions
|
||||||
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.relatedItem.trim());
|
t.equal(feed.xml(), expectedOutput.relatedItem.trim());
|
||||||
@@ -64,12 +60,7 @@ test('related item', function (t) {
|
|||||||
|
|
||||||
test('related item', function (t) {
|
test('related item', function (t) {
|
||||||
t.plan(1);
|
t.plan(1);
|
||||||
let feed = new TR({
|
let feed = new TR(baseOptions);
|
||||||
title: 'title',
|
|
||||||
description: 'description',
|
|
||||||
link: 'http://example.com/rss.xml',
|
|
||||||
site_url: 'http://example.com'
|
|
||||||
});
|
|
||||||
|
|
||||||
feed.item({
|
feed.item({
|
||||||
title: 'item title',
|
title: 'item title',
|
||||||
@@ -79,15 +70,7 @@ test('related item', function (t) {
|
|||||||
date: 'May 27, 2018 00:00 AM',
|
date: 'May 27, 2018 00:00 AM',
|
||||||
content: '<p>hello</p>',
|
content: '<p>hello</p>',
|
||||||
relatedfinity: true,
|
relatedfinity: true,
|
||||||
related: [{
|
related: relatedOptions
|
||||||
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.relatedItemInfinity.trim());
|
t.equal(feed.xml(), expectedOutput.relatedItemInfinity.trim());
|
||||||
@@ -95,12 +78,7 @@ test('related item', function (t) {
|
|||||||
|
|
||||||
test('menu', function (t) {
|
test('menu', function (t) {
|
||||||
t.plan(1);
|
t.plan(1);
|
||||||
let feed = new TR({
|
let feed = new TR(baseOptions);
|
||||||
title: 'title',
|
|
||||||
description: 'description',
|
|
||||||
link: 'http://example.com/rss.xml',
|
|
||||||
site_url: 'http://example.com'
|
|
||||||
});
|
|
||||||
|
|
||||||
feed.item({
|
feed.item({
|
||||||
title: 'item title',
|
title: 'item title',
|
||||||
@@ -117,15 +95,7 @@ test('menu', function (t) {
|
|||||||
link: 'http://example.com/about',
|
link: 'http://example.com/about',
|
||||||
text: 'О сайте',
|
text: 'О сайте',
|
||||||
}],
|
}],
|
||||||
related: [{
|
related: relatedOptions
|
||||||
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'
|
|
||||||
}]
|
|
||||||
});
|
});
|
||||||
|
|
||||||
feed.item({});
|
feed.item({});
|
||||||
@@ -135,12 +105,7 @@ test('menu', function (t) {
|
|||||||
|
|
||||||
test('goals', function (t) {
|
test('goals', function (t) {
|
||||||
t.plan(1);
|
t.plan(1);
|
||||||
let feed = new TR({
|
let feed = new TR(baseOptions);
|
||||||
title: 'title',
|
|
||||||
description: 'description',
|
|
||||||
link: 'http://example.com/rss.xml',
|
|
||||||
site_url: 'http://example.com'
|
|
||||||
});
|
|
||||||
|
|
||||||
feed.item({
|
feed.item({
|
||||||
title: 'item title',
|
title: 'item title',
|
||||||
@@ -150,21 +115,13 @@ test('goals', function (t) {
|
|||||||
date: 'May 27, 2018 00:00 AM',
|
date: 'May 27, 2018 00:00 AM',
|
||||||
menu: '<a href="http://example.com/page1.html">Текст ссылки</a> <a href="http://example.com/page2.html">Текст ссылки</a>',
|
menu: '<a href="http://example.com/page1.html">Текст ссылки</a> <a href="http://example.com/page2.html">Текст ссылки</a>',
|
||||||
goals: [{
|
goals: [{
|
||||||
type: "yandex",
|
type: 'yandex',
|
||||||
id: "turbo-goal-id",
|
id: 'turbo-goal-id',
|
||||||
counter_id: "12345",
|
counter_id: '12345',
|
||||||
name: "order",
|
name: 'order',
|
||||||
}],
|
}],
|
||||||
content: '<p>hello</p>',
|
content: '<p>hello</p>',
|
||||||
related: [{
|
related: relatedOptions
|
||||||
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());
|
t.equal(feed.xml(), expectedOutput.goal.trim());
|
||||||
|
|||||||
Reference in New Issue
Block a user