Skip to content

Commit a904ee3

Browse files
authored
Merge branch 'master' into propagate-page-cdn-attr
2 parents fe172f1 + 15628d1 commit a904ee3

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, asciidocConfig)
2627
knowledgeBase.addTagPages(pages, contentCatalog, asciidocConfig)
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, asciidocConfig) {
57
const pagesPerCategory = pages
@@ -109,7 +111,44 @@ function createKnowledgeBaseTagPage (tag, pages, asciidocConfig) {
109111
}
110112
}
111113

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+
112150
module.exports = {
113151
addCategoryPages: addKnowledgeBaseCategoryPages,
114-
addTagPages: addKnowledgeBaseTagPages
152+
addTagPages: addKnowledgeBaseTagPages,
153+
generateKnowledgeBasePageDescription: generateKnowledgeBasePageDescription
115154
}

0 commit comments

Comments
 (0)