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