forked from YMFE/ydoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.js
39 lines (35 loc) · 896 Bytes
/
page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const dom = require('./dom.js');
const utils = require('../utils');
let ids = [];
function handleArchor($){
let prefix = '';
$('h2,h3').each(function(){
let tagname = $(this).get(0).tagName;
let text = $(this).text();
if(tagname === 'h2'){
prefix = text + '-';
}else{
text = prefix + text;
}
let id = utils.hashEncode(text);
if(ids.indexOf(id) === -1){
ids.push(id);
}else{
utils.log.warn(`The document ${tagname} title: "${text}" repeated.`)
}
id = id.toLowerCase();
$(this).attr('id', id)
})
return $.html()
}
module.exports = function parsePage(html, archor){
const $ = dom.parse(html);
let page = {
title: $('h1:first-child').text().trim(),
description: $('div.paragraph,p').first().text().trim(),
content: html
};
ids = [];
if(archor)page.content = handleArchor($);
return page;
}