|
1 | 1 | 'use strict' |
2 | 2 |
|
| 3 | +const cheerio = require('cheerio') |
| 4 | + |
3 | 5 | // tag::add-category-pages[] |
4 | 6 | function addKnowledgeBaseCategoryPages (pages, contentCatalog) { |
5 | 7 | const pagesPerCategory = pages |
@@ -106,7 +108,44 @@ function createKnowledgeBaseTagPage (tag, pages) { |
106 | 108 | } |
107 | 109 | } |
108 | 110 |
|
| 111 | +const generateExcerpt = (page) => { |
| 112 | + const $ = cheerio.load(page.contents.toString()) |
| 113 | + // removes tables, images and source block |
| 114 | + $('table').remove() |
| 115 | + $('img').remove() |
| 116 | + $('pre').remove() |
| 117 | + $('iframe').remove() |
| 118 | + let text = $.text() |
| 119 | + text = text.trim() |
| 120 | + .replace(/\n/g, ' ') |
| 121 | + .replace(/\s{2,}/g, ' ') |
| 122 | + let words = text.split(' ') |
| 123 | + let excerpt |
| 124 | + if (words.length > 30) { |
| 125 | + words = words.slice(0, 30) |
| 126 | + excerpt = words.join(' ') + '…' |
| 127 | + } else { |
| 128 | + excerpt = words.join(' ') |
| 129 | + } |
| 130 | + return excerpt |
| 131 | +} |
| 132 | + |
| 133 | +function generateKnowledgeBasePageDescription(pages) { |
| 134 | + pages |
| 135 | + .filter((page) => { |
| 136 | + return page.src.component === 'kb' && |
| 137 | + page.src.basename !== 'index.adoc' && |
| 138 | + page.asciidoc && |
| 139 | + page.asciidoc.attributes && |
| 140 | + !page.asciidoc.attributes['page-description'] |
| 141 | + }) |
| 142 | + .forEach((page) => { |
| 143 | + page.asciidoc.attributes['page-description'] = generateExcerpt(page) |
| 144 | + }) |
| 145 | +} |
| 146 | + |
109 | 147 | module.exports = { |
110 | 148 | addCategoryPages: addKnowledgeBaseCategoryPages, |
111 | | - addTagPages: addKnowledgeBaseTagPages |
| 149 | + addTagPages: addKnowledgeBaseTagPages, |
| 150 | + generateKnowledgeBasePageDescription: generateKnowledgeBasePageDescription |
112 | 151 | } |
0 commit comments