1 | const dom = require('./dom.js');
|
2 | const utils = require('../utils');
|
3 | let ids = [];
|
4 |
|
5 | function handleArchor($){
|
6 | let prefix = '';
|
7 | $('h2,h3').each(function(){
|
8 | let tagname = $(this).get(0).tagName;
|
9 | let text = $(this).text();
|
10 | if(tagname === 'h2'){
|
11 | prefix = text + '-';
|
12 | }else{
|
13 | text = prefix + text;
|
14 | }
|
15 | let id = utils.hashEncode(text);
|
16 | if(ids.indexOf(id) === -1){
|
17 | ids.push(id);
|
18 | }else{
|
19 | utils.log.warn(`The document ${tagname} title: "${text}" repeated.`)
|
20 | }
|
21 | id = id.toLowerCase();
|
22 | $(this).attr('id', id)
|
23 | })
|
24 |
|
25 | return $.html()
|
26 | }
|
27 |
|
28 | module.exports = function parsePage(html, archor){
|
29 | const $ = dom.parse(html);
|
30 | let page = {
|
31 | title: $('h1:first-child').text().trim(),
|
32 | description: $('div.paragraph,p').first().text().trim(),
|
33 | content: html
|
34 | };
|
35 | ids = [];
|
36 |
|
37 | if(archor)page.content = handleArchor($);
|
38 | return page;
|
39 | } |
\ | No newline at end of file |