Skip to content

Commit 7351c43

Browse files
committed
resolves #4 generate page-description on KB articles
1 parent 2234a2a commit 7351c43

File tree

5 files changed

+965
-3
lines changed

5 files changed

+965
-3
lines changed

lib/generate-site.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ async function generateSite (args, env) {
2222
loadUi(playbook),
2323
])
2424
const pages = convertDocuments(contentCatalog, asciidocConfig)
25+
knowledgeBase.generateKnowledgeBasePageDescription(pages)
2526
knowledgeBase.addCategoryPages(pages, contentCatalog)
2627
knowledgeBase.addTagPages(pages, contentCatalog)
2728
const navigationCatalog = buildNavigation(contentCatalog, asciidocConfig)

lib/knowledge-base.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const cheerio = require('cheerio')
4+
35
// tag::add-category-pages[]
46
function addKnowledgeBaseCategoryPages (pages, contentCatalog) {
57
const pagesPerCategory = pages
@@ -106,7 +108,44 @@ function createKnowledgeBaseTagPage (tag, pages) {
106108
}
107109
}
108110

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+
109147
module.exports = {
110148
addCategoryPages: addKnowledgeBaseCategoryPages,
111-
addTagPages: addKnowledgeBaseTagPages
149+
addTagPages: addKnowledgeBaseTagPages,
150+
generateKnowledgeBasePageDescription: generateKnowledgeBasePageDescription
112151
}

0 commit comments

Comments
 (0)