From bd80574f76dc498aef06910fe186966ebf41eac6 Mon Sep 17 00:00:00 2001 From: Atlas Quan Date: Mon, 4 Jul 2022 23:44:58 +0800 Subject: [PATCH] feat(route): add slowmist news (#10044) * feat(route): add slowmist news * feat(utils): remove useless ul for code section * docs: add slowmist news route docs * docs(quick-start): new router under /lib/v2 * test(utils): add test fixArticleContent for new wechat-mp code * feat(router): update radar source of slowmist --- docs/joinus/quick-start.md | 6 +++--- docs/new-media.md | 12 ++++++++++++ lib/utils/wechat-mp.js | 10 ++++++++-- lib/v2/slowmist/radar.js | 13 +++++++++++++ lib/v2/slowmist/router.js | 3 +++ lib/v2/slowmist/slowmist.js | 39 +++++++++++++++++++++++++++++++++++++ test/utils/wechat-mp.js | 25 ++++++++++++++++++++++-- 7 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 lib/v2/slowmist/radar.js create mode 100644 lib/v2/slowmist/router.js create mode 100644 lib/v2/slowmist/slowmist.js diff --git a/docs/joinus/quick-start.md b/docs/joinus/quick-start.md index 934279f18..ed972fed4 100644 --- a/docs/joinus/quick-start.md +++ b/docs/joinus/quick-start.md @@ -21,11 +21,11 @@ sidebar: auto ### 添加脚本路由 -在 [/lib/router.js](https://github.com/DIYgod/RSSHub/blob/master/lib/router.js) 里添加路由 +在 [/lib/v2/routes/](https://github.com/DIYgod/RSSHub/tree/master/lib/routes) 中创建对应路由路径,并在 `/lib/v2/routes/:path/router.js` 中添加路由 ### 编写脚本 -在 [/lib/routes/](https://github.com/DIYgod/RSSHub/tree/master/lib/routes) 中的路由对应路径下创建新的 js 脚本: +在 [/lib/v2/routes/](https://github.com/DIYgod/RSSHub/tree/master/lib/routes) 中的路由对应路径下创建新的 js 脚本: #### 获取源数据 @@ -493,7 +493,7 @@ ctx.state.data = { ### 编写规则 -在 [/assets/radar-rules.js](https://github.com/DIYgod/RSSHub/blob/master/assets/radar-rules.js) 里添加规则 +在 [/lib/v2/routes/](https://github.com/DIYgod/RSSHub/tree/master/lib/routes) 的对应路由下创建 `radar.js` 并添加规则 下面说明中会用到的简化的规则: diff --git a/docs/new-media.md b/docs/new-media.md index 766124410..61a4f8908 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -2666,6 +2666,18 @@ column 为 third 时可选的 category: +## 慢雾科技 + +### 动态 + + + +| 公司新闻 | 漏洞披露 | 技术研究 | +| ---- | ---- | -------- | +| news | vul | research | + + + ## 梅花网 ### 作品 diff --git a/lib/utils/wechat-mp.js b/lib/utils/wechat-mp.js index 53deb80f8..67916f9a0 100644 --- a/lib/utils/wechat-mp.js +++ b/lib/utils/wechat-mp.js @@ -104,6 +104,14 @@ const fixArticleContent = (html, skipImg = false) => { } }); + // add breaks in code section + $('code').each((_, code) => { + $('
').insertAfter(code); + }); + + // clear line index tags in code section + $('.code-snippet__line-index').remove(); + // fix single picture article // example: https://mp.weixin.qq.com/s/4p5YmYuASiQSYFiy7KqydQ $('script').each((_, script) => { @@ -186,7 +194,6 @@ const fetchArticle = async (ctx, url, bypassHostCheck = false) => { let summary = $('meta[name=description]').attr('content'); summary = summary !== title ? summary : ''; let description = fixArticleContent($('#js_content')); - // No article get or article is too short, try the original url const originalUrl = detectOriginalArticleUrl($); if (originalUrl) { @@ -211,7 +218,6 @@ const fetchArticle = async (ctx, url, bypassHostCheck = false) => { let mpName = $('.profile_nickname').first().text(); mpName = mpName && mpName.trim(); - return { title, author, description, summary, pubDate, mpName, link: url }; }); }; diff --git a/lib/v2/slowmist/radar.js b/lib/v2/slowmist/radar.js new file mode 100644 index 000000000..2bdf100e2 --- /dev/null +++ b/lib/v2/slowmist/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'slowmist.com': { + _name: '慢雾科技 SLOWMIST', + '.': [ + { + title: '动态', + docs: 'https://docs.rsshub.app/new-media.html#man-wu-ke-ji', + source: ['/zh/news.html'], + target: '/slowmist/:type?', + }, + ], + }, +}; diff --git a/lib/v2/slowmist/router.js b/lib/v2/slowmist/router.js new file mode 100644 index 000000000..7439f0373 --- /dev/null +++ b/lib/v2/slowmist/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/:type?', require('./slowmist')); +}; diff --git a/lib/v2/slowmist/slowmist.js b/lib/v2/slowmist/slowmist.js new file mode 100644 index 000000000..7c2e99220 --- /dev/null +++ b/lib/v2/slowmist/slowmist.js @@ -0,0 +1,39 @@ +const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); +const baseUrl = 'https://www.slowmist.com'; +const { finishArticleItem } = require('@/utils/wechat-mp'); + +module.exports = async (ctx) => { + let type = ctx.params.type; + + let title = '慢雾科技 - '; + if (type === 'news') { + title += '公司新闻'; + } else if (type === 'vul') { + title += '漏洞披露'; + } else if (type === 'research') { + title += '技术研究'; + } else { + type = 'news'; + title += '公司新闻'; + } + + const url = `${baseUrl}/api/get_list?type=${type}`; + + const response = await got(url); + + let items = (response.data.data || []).map((item) => ({ + title: item.title, + link: item.url, + description: item.desc, + pubDate: parseDate(item.date), + })); + + items = await Promise.all(items.map((item) => finishArticleItem(ctx, item))); + + ctx.state.data = { + title, + link: url, + item: items, + }; +}; diff --git a/test/utils/wechat-mp.js b/test/utils/wechat-mp.js index 2be41f35e..d0719bf60 100644 --- a/test/utils/wechat-mp.js +++ b/test/utils/wechat-mp.js @@ -24,8 +24,28 @@ describe('wechat-mp', () => { it('fixArticleContent', () => { const divHeader = '
'; const divFooter = '
'; - + const codeSection = + '
' + + '' + + '
' +
+            'Line1 {' +
+            'Line2' +
+            'Line3 }' +
+            '
'; + const expectedCodeSection = + '

' + + '

' +
+            'Line1 {' +
+            '
' + + 'Line2' + + '
' + + 'Line3 }' + + '
' + + '

'; const htmlSection = + codeSection + '
test
' + '

test

' + '
test
' + @@ -34,7 +54,8 @@ describe('wechat-mp', () => { '

test

' + '

test

' + ''; - const expectedHtmlSection = '

test

' + '

test

' + '
test
' + '

test

' + '

test

' + '

test

' + '

test

'; + const expectedHtmlSection = + expectedCodeSection + '

test

' + '

test

' + '
test
' + '

test

' + '

test

' + '

test

' + '

test

'; let $ = cheerio.load(divHeader + htmlSection + divFooter); expect(fixArticleContent(htmlSection)).toBe(expectedHtmlSection); expect(fixArticleContent($('div#js_content.rich_media_content'))).toBe(expectedHtmlSection);