/*! * sections * * Copyright (c) 2015, 2017, Jon Schlinkert. * Released under the MIT License. */ 'use strict'; var sortBy = require('sort-by-value'); var gfm = require('gfm-code-blocks'); /** * Parses sections in a `string` of markdown and returns an object * with two properties: * * - `sections`: an array of markdown "sections", delimited by [ATX headings][atx], * - `result`: the cumulative result of whatever is returned by the (optional) function that is passed as the second argument. * * Returns an object that looks [something like this](#example-object) * * ```js * var fs = require('fs'); * var readme = fs.readFileSync('readme.md', 'utf8'); * var sections = require('sections'); * console.log(sections.parse(readme)); * ``` * @param {String} `string` * @param {Function} `fn` * @return {Object} * @api public */ exports.parse = function(str, fn) { if (typeof str !== 'string') { throw new TypeError('expected a string'); } var sections = str.split(/(?=\n(?:#|/.exec(title); if (match) { title = match[1].trim(); } return title; } function hasBadge(idx, title) { return idx === 0 && title.indexOf('[![') !== -1; } function badge(section) { var title = section.title; title = title.slice(0, title.indexOf('[![')); if (!section.hasOwnProperty('badges')) { section.badges = []; } section.badges.push(title.slice(title.length).trim()); section.title = title.trim(); } function emit(str) { return '\n' + toString(str) + '\n'; } function endingWs(str) { var m = /(\s+)$/.exec(str) || []; return m[1] || ''; } function toString(str) { return String(str || '').trim(); } /** * Expose `emit` */ exports.emit = emit;