1
0
mirror of synced 2025-12-26 14:02:45 -05:00
Files
docs/lib/search/parse-page-sections-into-records.js
Kevin Heis 2fb2e962bc Move site search to use an endpoint (#17359)
* Move site search to use an endpoint

* Update browser.js

* Update search.js

* Update lib/search/versions.js

Co-authored-by: James M. Greene <JamesMGreene@github.com>

* Fix URLs

Co-authored-by: James M. Greene <JamesMGreene@github.com>
2021-01-20 15:37:42 +00:00

85 lines
2.2 KiB
JavaScript

// This module takes cheerio page object and divides it into sections
// using H1,h2,h3 heading elements as section delimiters. The text
// that follows each heading becomes the content of the search record.
const { chain } = require('lodash')
const urlPrefix = 'https://docs.github.com'
const ignoredHeadingSlugs = [
'in-this-article',
'further-reading'
]
const { maxContentLength } = require('./config')
module.exports = function parsePageSectionsIntoRecords (href, $) {
const title = $('h1').text().trim()
const breadcrumbs = $('nav.breadcrumbs a')
.map((i, el) => {
return $(el)
.text()
.trim()
.replace(/\n/g, ' ')
.replace(/\s+/g, ' ')
})
.get()
.join(' / ')
let records
const $sections = $('.article-grid-body h3')
.filter('[id]')
.filter((i, el) => {
return !ignoredHeadingSlugs.includes($(el).attr('id'))
})
if ($sections.length > 0) {
records = $sections
.map((i, el) => {
const heading = $(el).text().trim()
const slug = $(el).attr('id')
const objectID = [href, slug].join('#')
const url = [urlPrefix, objectID].join('')
const content = $(el)
// Platform-specific content is nested in a DIV
// GraphQL content in nested in two DIVS
.nextUntil('h2, h3, div > h2, div > h3, div > div > h2, div > div > h3')
.map((i, el) => $(el).text())
.get()
.join(' ')
.trim()
.slice(0, maxContentLength)
return {
objectID,
url,
slug,
breadcrumbs,
heading,
title,
content
}
})
.get()
} else {
// There are no sections. Treat the entire article as the record.
const objectID = href
const url = [urlPrefix, objectID].join('')
const content = $('.article-grid-body p, .article-grid-body ul, .article-grid-body ol, .article-grid-body table')
.map((i, el) => $(el).text())
.get()
.join(' ')
.trim()
.slice(0, maxContentLength)
records = [{
objectID,
url,
breadcrumbs,
title,
content
}]
}
return chain(records)
.uniqBy('objectID')
.value()
}