|
| 1 | +import * as detectNewline from 'detect-newline' |
| 2 | +import { safeLoad } from 'js-yaml' |
| 3 | + |
| 4 | +/** |
| 5 | + * Configuration that can be supplied by the user |
| 6 | + */ |
| 7 | +export interface MarkdownParserConfig { |
| 8 | + windows: boolean /** Specify whether to treat the string as though from a windows platform */ |
| 9 | +} |
| 10 | + |
| 11 | +export interface AnyMap { |
| 12 | + [x: string]: any |
| 13 | +} |
| 14 | + |
| 15 | +export interface MarkdownData { |
| 16 | + metadata: AnyMap |
| 17 | + content: string |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Internal Operation Configurations |
| 22 | + */ |
| 23 | +interface InternalConfig extends MarkdownParserConfig { |
| 24 | + source: string /** A string containing the markdown */ |
| 25 | + isWin(): boolean |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * The configuration to be used for operation |
| 30 | + */ |
| 31 | +const config: InternalConfig = { |
| 32 | + source: '', |
| 33 | + windows: false, |
| 34 | + // infer the platform type by the eol present in the source string |
| 35 | + isWin: () => { |
| 36 | + const newLine = detectNewline(config.source) |
| 37 | + return config.windows || (!!newLine && newLine.match(/\r\n/) !== null) |
| 38 | + }, |
| 39 | +} |
| 40 | + |
| 41 | +Object.seal(config) |
| 42 | + |
| 43 | +const METADATA_START = () => (config.isWin() ? /^---\r\n/ : /^---\n/) |
| 44 | +const METADATA_END = () => (config.isWin() ? /\r\n---\r\n/ : /\n---\n/) |
| 45 | +const METADATA_FILE_END = () => (config.isWin() ? /\r\n---$/ : /\n---$/) |
| 46 | +const JOIN_SEPARATOR = () => (config.isWin() ? '\r\n---\r\n' : '\n---\n') |
| 47 | + |
| 48 | +/** |
| 49 | + * Check if the provided array has only one element that ends with METADATA_FILE_END. |
| 50 | + * If so, the source is metadata only with no content. The function cleans the metadata and adds an empty content element to the array. |
| 51 | + * |
| 52 | + * @param {Array} |
| 53 | + * @returns {Array} |
| 54 | + */ |
| 55 | +const checkMetadataOnly = (src: string[]) => { |
| 56 | + if (src.length === 1 && src[0].match(METADATA_FILE_END()) !== null) { |
| 57 | + return [src[0].replace(METADATA_FILE_END(), ''), ''] |
| 58 | + } |
| 59 | + return src |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Split a string with the METADATA_END separator if it starts with METADATA_START. |
| 64 | + * Otherwise it creates an array containing the source string provided. |
| 65 | + * |
| 66 | + * @param {string} Source string to split. |
| 67 | + * @returns {Array} |
| 68 | + */ |
| 69 | +const splitSource = (src: string) => { |
| 70 | + if (src.match(METADATA_START()) !== null) { |
| 71 | + return checkMetadataOnly(src.split(METADATA_END())) |
| 72 | + } |
| 73 | + return [src] |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * If source array has more than one value, it cleans (remove METADATA_START() and trim) and returns the first one. |
| 78 | + * Otherwise it returns null. |
| 79 | + * |
| 80 | + * @param {string[]} src |
| 81 | + * @returns {string|null} |
| 82 | + */ |
| 83 | +const cleanMetadata = (src: string[]) => { |
| 84 | + if (src.length >= 1) { |
| 85 | + return src[0].replace(METADATA_START(), '').trim() |
| 86 | + } |
| 87 | + return '' |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * If the supplied value is nil, it returns an empty object, otherwise it returns the value itself. |
| 92 | + * |
| 93 | + * @param {string} src |
| 94 | + * @returns {string | object} |
| 95 | + */ |
| 96 | +const emptyObjectIfNil = (src: string) => (src.length === 0 ? {} : src) |
| 97 | + |
| 98 | +/** |
| 99 | + * Join the elements of the array except the first one (metadata). |
| 100 | + * If there's only one element (no metadata), it returns it. |
| 101 | + * |
| 102 | + * @param {string[]} srcLines |
| 103 | + * @returns {string} |
| 104 | + */ |
| 105 | +const joinContent = (srcLines: string[]) => { |
| 106 | + if (srcLines.length > 1) { |
| 107 | + return srcLines.slice(1, srcLines.length).join(JOIN_SEPARATOR()) |
| 108 | + } |
| 109 | + return srcLines.join('') |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Validate incoming input. |
| 114 | + * |
| 115 | + * @param {string} src Document source to parse. |
| 116 | + * @param {MarkdownParserConfig} config Operation configuration. |
| 117 | + */ |
| 118 | + |
| 119 | +const validateInput = (src: string, config: MarkdownParserConfig) => { |
| 120 | + if (typeof src !== 'string') { |
| 121 | + throw new TypeError('Source parameter (src) must be a string.') |
| 122 | + } |
| 123 | + |
| 124 | + if (Object.keys(config).length > 0 && typeof config.windows !== 'boolean') { |
| 125 | + throw new TypeError('Configuration property (windows) must be a boolean.') |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Parse a markdown document (src) looking for metadata in YAML format. |
| 131 | + * In order to be parsed, metadata must be placed at the beginning of the document between two triple dashes. |
| 132 | + * Example: |
| 133 | + * --- |
| 134 | + * title: Lorem ipsum |
| 135 | + * author: Marcus Antonius |
| 136 | + * keywords: latin, ipsum |
| 137 | + * --- |
| 138 | + * |
| 139 | + * NB: setting windows to true in configuration prop will override the ability |
| 140 | + * to infer the type from the document (src) |
| 141 | + * |
| 142 | + * @param {string} src Document source to parse. |
| 143 | + * @param {MarkdownParserConfig} config Operation configuration. |
| 144 | + * @returns {MarkdownData} |
| 145 | + * @throws {TypeError} src must be a string. |
| 146 | + * @throws {YAMLException} Error on YAML metadata parsing. |
| 147 | + */ |
| 148 | +export const parse = ( |
| 149 | + src: string, |
| 150 | + userConfig: MarkdownParserConfig = config |
| 151 | +): MarkdownData => { |
| 152 | + validateInput(src, userConfig) |
| 153 | + |
| 154 | + config.source = src.trim() |
| 155 | + |
| 156 | + config.windows = userConfig.windows |
| 157 | + |
| 158 | + const splittedSource = splitSource(config.source) |
| 159 | + |
| 160 | + const cleanedMetadata = cleanMetadata(splittedSource) |
| 161 | + const parsedYaml = safeLoad(cleanedMetadata) |
| 162 | + const metaData = emptyObjectIfNil(parsedYaml) |
| 163 | + |
| 164 | + const content = joinContent(splittedSource) |
| 165 | + |
| 166 | + return { |
| 167 | + metadata: metaData, |
| 168 | + content: content, |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +export default parse |
0 commit comments