Skip to content
Permalink
Browse files

feat: extract headers from yuque's HTML markups

  • Loading branch information...
ulivz committed Feb 8, 2019
1 parent 85801e7 commit 88900aee0220b61a2b8ce81891414a61b2751dd0
Showing with 125 additions and 17 deletions.
  1. +13 −0 lib/compose.js
  2. +54 −0 lib/html.js
  3. +2 −1 lib/index.js
  4. +0 −6 lib/yuque.js
  5. +6 −5 package.json
  6. +50 −5 yarn.lock
@@ -0,0 +1,13 @@
/**
* Expose compose.
*/

module.exports = function compose(...processors) {
if (processors.length === 0)
return (input) => input
if (processors.length === 1)
return processors[0]
return processors.reduce((prev, next) => {
return (...args) => next(prev(...args))
})
}
@@ -0,0 +1,54 @@
/**
* Module dependencies.
*/

const cheerio = require('cheerio')
const compose = require('./compose')

/**
* Expose a object containing the utilities of handling markups.
*/

module.exports = {
prettify
}

const transformHeaders = compose(
...[
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
].map((h, i) => $ => {
$(h).replaceWith((_i, el) => {
return `\n\n ${getHash(i + 1)} ${$(el).text()} \n\n`
})
return $
}
)
)

function getHash(i) {
return [...Array(i)].map(() => '#').join('')
}

function prettify(html) {
html = prune(html)
const $ = cheerio.load(html, {
// ref: https://github.com/cheeriojs/cheerio/issues/565
decodeEntities: false
})
transformHeaders($)
return $('body').html()
}

function prune(html) {
return html
.replace(/<p><br\s\/><\/p>/g, '')
.replace(/<p><span><br\s\/><\/span><\/p>/g, '')
}



@@ -7,6 +7,7 @@ const { chalk, parseFrontmatter, inferTitle } = require('@vuepress/shared-utils'
const Yuque = require('./yuque')
const { getSidebarByToc } = require('./toc')
const spinner = require('./spinner')
const { prettify } = require('./html')
const { PACKAGE_NAME } = require('./constant')
const debug = require('debug')(PACKAGE_NAME)

@@ -124,7 +125,7 @@ module.exports = (opts, ctx) => {
if (useMarkdown) {
postContent = data.body
} else {
postContent = Yuque.beautifyHTML(data.body_html)
postContent = prettify(data.body_html)
}
}

@@ -64,12 +64,6 @@ module.exports = class Yuque {
}
}

static beautifyHTML(html) {
return html
.replace(/<p><br\s\/><\/p>/g, '')
.replace(/<p><span><br\s\/><\/span><\/p>/g, '')
}

static async get(base, path) {
path = `${base}${path}`
return Yuque.fetch(path)
@@ -19,20 +19,21 @@
"author": "ulivz<chl814@foxmail.com>",
"license": "MIT",
"dependencies": {
"node-fetch": "^2.3.0",
"cheerio": "^1.0.0-rc.2",
"conf": "^2.2.0",
"debug": "^4.1.1",
"node-fetch": "^2.3.0",
"ora": "^3.0.0"
},
"devDependencies": {
"xo": "0.23.0",
"conventional-changelog-cli": "^2.0.1",
"eslint-config-sherry": "0.0.1",
"husky": "1.2.0",
"lint-staged": "8.1.0",
"jest": "23.6.0",
"vuepress": "1.0.0-alpha.36",
"lint-staged": "8.1.0",
"release-it": "v7.4.8",
"conventional-changelog-cli": "^2.0.1"
"vuepress": "1.0.0-alpha.36",
"xo": "0.23.0"
},
"jest": {
"testEnvironment": "node"
@@ -638,6 +638,10 @@
version "0.7.0"
resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.7.0.tgz#9a06f4f137ee84d7df0460c1fdb1135ffa6c50fd"

"@types/node@*":
version "10.12.23"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.23.tgz#308a3acdc5d1c842aeadc50b867d99c46cfae868"

"@vue/babel-helper-vue-jsx-merge-props@^1.0.0-beta.2":
version "1.0.0-beta.2"
resolved "https://registry.yarnpkg.com/@vue/babel-helper-vue-jsx-merge-props/-/babel-helper-vue-jsx-merge-props-1.0.0-beta.2.tgz#f3e20d77b89ddb7a4b9b7a75372f05cd3ac22d92"
@@ -2319,6 +2323,17 @@ chardet@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"

cheerio@^1.0.0-rc.2:
version "1.0.0-rc.2"
resolved "https://registry.yarnpkg.com/cheerio/-/cheerio-1.0.0-rc.2.tgz#4b9f53a81b27e4d5dac31c0ffd0cfa03cc6830db"
dependencies:
css-select "~1.2.0"
dom-serializer "~0.1.0"
entities "~1.1.1"
htmlparser2 "^3.9.1"
lodash "^4.15.0"
parse5 "^3.0.1"

chokidar@^2.0.0, chokidar@^2.0.2, chokidar@^2.0.3:
version "2.0.4"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-2.0.4.tgz#356ff4e2b0e8e43e322d18a372460bbcf3accd26"
@@ -3025,7 +3040,7 @@ css-parse@1.7.x:
version "1.7.0"
resolved "https://registry.yarnpkg.com/css-parse/-/css-parse-1.7.0.tgz#321f6cf73782a6ff751111390fc05e2c657d8c9b"

css-select@^1.1.0:
css-select@^1.1.0, css-select@~1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/css-select/-/css-select-1.2.0.tgz#2b3a110539c5355f1cd8d314623e870b121ec858"
dependencies:
@@ -3416,7 +3431,7 @@ dom-converter@~0.2:
dependencies:
utila "~0.4"

dom-serializer@0:
dom-serializer@0, dom-serializer@~0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/dom-serializer/-/dom-serializer-0.1.0.tgz#073c697546ce0780ce23be4a28e293e40bc30c82"
dependencies:
@@ -3431,7 +3446,7 @@ domain-browser@^1.1.1:
version "1.2.0"
resolved "https://registry.yarnpkg.com/domain-browser/-/domain-browser-1.2.0.tgz#3d31f50191a6749dd1375a7f522e823d42e54eda"

domelementtype@1:
domelementtype@1, domelementtype@^1.3.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/domelementtype/-/domelementtype-1.3.1.tgz#d048c44b37b0d10a7f2a3d5fee3f4333d790481f"

@@ -3451,6 +3466,12 @@ domhandler@2.1:
dependencies:
domelementtype "1"

domhandler@^2.3.0:
version "2.4.2"
resolved "https://registry.yarnpkg.com/domhandler/-/domhandler-2.4.2.tgz#8805097e933d65e85546f726d60f5eb88b44f803"
dependencies:
domelementtype "1"

domutils@1.1:
version "1.1.6"
resolved "https://registry.yarnpkg.com/domutils/-/domutils-1.1.6.tgz#bddc3de099b9a2efacc51c623f28f416ecc57485"
@@ -3464,6 +3485,13 @@ domutils@1.5.1:
dom-serializer "0"
domelementtype "1"

domutils@^1.5.1:
version "1.7.0"
resolved "https://registry.yarnpkg.com/domutils/-/domutils-1.7.0.tgz#56ea341e834e06e6748af7a1cb25da67ea9f8c2a"
dependencies:
dom-serializer "0"
domelementtype "1"

dot-prop@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/dot-prop/-/dot-prop-3.0.0.tgz#1b708af094a49c9a0e7dbcad790aba539dac1177"
@@ -3552,7 +3580,7 @@ enhanced-resolve@^4.1.0:
memory-fs "^0.4.0"
tapable "^1.0.0"

entities@~1.1.1:
entities@^1.1.1, entities@~1.1.1:
version "1.1.2"
resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.2.tgz#bdfa735299664dfafd34529ed4f8522a275fea56"

@@ -4831,6 +4859,17 @@ html-tags@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/html-tags/-/html-tags-2.0.0.tgz#10b30a386085f43cede353cc8fa7cb0deeea668b"

htmlparser2@^3.9.1:
version "3.10.0"
resolved "https://registry.yarnpkg.com/htmlparser2/-/htmlparser2-3.10.0.tgz#5f5e422dcf6119c0d983ed36260ce9ded0bee464"
dependencies:
domelementtype "^1.3.0"
domhandler "^2.3.0"
domutils "^1.5.1"
entities "^1.1.1"
inherits "^2.0.1"
readable-stream "^3.0.6"

htmlparser2@~3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/htmlparser2/-/htmlparser2-3.3.0.tgz#cc70d05a59f6542e43f0e685c982e14c924a9efe"
@@ -6339,7 +6378,7 @@ lodash@4.17.10:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"

lodash@^4.13.1, lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.1, lodash@^4.3.0:
lodash@^4.13.1, lodash@^4.15.0, lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.1, lodash@^4.3.0:
version "4.17.11"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"

@@ -7418,6 +7457,12 @@ parse5@4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/parse5/-/parse5-4.0.0.tgz#6d78656e3da8d78b4ec0b906f7c08ef1dfe3f608"

parse5@^3.0.1:
version "3.0.3"
resolved "https://registry.yarnpkg.com/parse5/-/parse5-3.0.3.tgz#042f792ffdd36851551cf4e9e066b3874ab45b5c"
dependencies:
"@types/node" "*"

parseurl@~1.3.2:
version "1.3.2"
resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.2.tgz#fc289d4ed8993119460c156253262cdc8de65bf3"

0 comments on commit 88900ae

Please sign in to comment.
You can’t perform that action at this time.