-
-
Save motleydev/2b529463c9371bb7adb16b9fa9d0e7a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
Object.defineProperty(exports, "__esModule", { | |
value: true | |
}); | |
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | |
var _remark = require("remark"); | |
var _remark2 = _interopRequireDefault(_remark); | |
var _remarkSlug = require("remark-slug"); | |
var _remarkSlug2 = _interopRequireDefault(_remarkSlug); | |
// var _remarkAutolinkHeadings = require("remark-autolink-headings"); | |
// var _remarkAutolinkHeadings2 = _interopRequireDefault(_remarkAutolinkHeadings); | |
var _remarkHighlight = require("remark-highlight.js"); | |
var _remarkHighlight2 = _interopRequireDefault(_remarkHighlight); | |
var _remarkHtml = require("remark-html"); | |
var _remarkHtml2 = _interopRequireDefault(_remarkHtml); | |
var wrapHeaders = require("./wrapHeaders"); | |
var _wrapHeaders2 = _interopRequireDefault(wrapHeaders); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
function mdify(text) { | |
return (0, _remark2.default)() | |
// https://github.com/wooorm/remark-slug | |
.use(_remarkSlug2.default) | |
.use(_wrapHeaders2.default) | |
// https://github.com/wooorm/remark-html | |
.use(_remarkHtml2.default, { entities: "escape" }) | |
// https://github.com/ben-eb/remark-highlight.js | |
.use(_remarkHighlight2.default) | |
// render | |
.process(text, { | |
commonmark: true | |
}).toString(); | |
} | |
exports.default = function (_ref) { | |
var result = _ref.result; | |
return _extends({}, result, { | |
head: _extends({}, result.head, { | |
summary: mdify(result.head.summary) | |
}), | |
body: mdify(result.body) | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Titus Wormer | |
* @copyright 2015 Titus Wormer | |
* @license MIT | |
* @module remark:slug | |
* @fileoverview Add anchors to remark heading nodes. | |
*/ | |
'use strict'; | |
/* Dependencies. */ | |
var toString = require('mdast-util-to-string'); | |
var visit = require('unist-util-visit'); | |
/* Expose. */ | |
module.exports = attacher; | |
/* Attacher. */ | |
function attacher() { | |
return transformer; | |
} | |
/* Patch slugs on heading nodes. */ | |
function transformer(ast) { | |
visit(ast, 'heading', function (node) { | |
let target = node.children[0]; | |
let {position, type, value} = target; | |
node.children = []; | |
// delete target.value; | |
let words = value.split(" "); | |
words.forEach((word, index) => { | |
node.children.push({ | |
type: 'element', | |
tagName: 'span', | |
children: [ | |
{ | |
type: "text", | |
value: word | |
} | |
] | |
// position: position | |
}) | |
if (index < words.length) { | |
node.children.push({ | |
type: 'element', | |
tagName: 'span', | |
children: [ | |
{ | |
type: "text", | |
value: " " | |
} | |
] | |
// position: position | |
}) | |
} | |
}); | |
}); | |
} | |
/** | |
* Patch `value` on `context` at `key`, if | |
* `context[key]` does not already exist. | |
* | |
* @param {Object} context - Context to patch. | |
* @param {string} key - Key to patch at. | |
* @param {*} value - Value to patch. | |
*/ | |
function patch(context, key, value) { | |
if (!context[key]) { | |
context[key] = value; | |
} | |
return context[key]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment