Last active
May 14, 2017 01:29
-
-
Save zacacollier/807d416820a5ea341000995ed0e4e6d7 to your computer and use it in GitHub Desktop.
JS Bin// source http://jsbin.com/gawobic
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
/* | |
* Reusable Helpers (á la Professor Frisby): | |
* Note that in a real project, these would live in a separate file | |
* (e.g. `src/constants/helpers.js`) | |
*/ | |
function split(string, onChar) { | |
return string.split(onChar); | |
} | |
/* Designate filtered stems for concatenating to the result */ | |
const stems = { | |
isOne: string => string.filter(s => +s === 1), | |
isNotOne: string => string.filter(s => +s !== 1), | |
}; | |
/* Build HTML 'h' tags */ | |
const html = (level) => ({ | |
h: { | |
open: `<h${level}>`, | |
close: `</h${level}>`, | |
} | |
}); | |
/* Regex Constants */ | |
const regex = { | |
hashes: /^\#/g, | |
letters: /[a-zA-Z0-9]/ | |
} | |
/* String Helpers (functional String.prototype method mappings) */ | |
const strFns = { | |
replace: (string, delimiter, char) => string.replace(delimiter, char), | |
join: (splitString, delimiter) => splitString.join(delimiter), | |
search: (string, delimiter, char) => string.search(delimiter), | |
split: (string, delimiter) => string.split(delimiter), | |
} | |
/* | |
* End Helper Section | |
*/ | |
function markdownParser(markdown) { | |
let { join, | |
replace, | |
search, | |
split, } = strFns | |
// Split the string | |
let splitMd = split(markdown, "") | |
// Convert the string's `prefix` | |
for (let i = 0; i < splitMd.length; i++) { | |
splitMd[i] = splitMd[i].replace(regex.hashes, "1"); | |
if (splitMd[i] === " ") { | |
break; | |
} | |
} | |
const { isOne, | |
isNotOne } = stems | |
const ones = isOne(splitMd) | |
const noOnes = isNotOne(splitMd) | |
let searchLetters = search(ones.join(""), regex.letters) | |
if (searchLetters > -1) { | |
// Revert from "1"'s back to "#"'s | |
const revert = ones | |
.map(e => e.replace("1", "#")) | |
// Rebuild the rest of the stem (after the `converted` section) | |
const subStem = join(noOnes | |
.slice(noOnes.indexOf(" "), noOnes.length) | |
, "") | |
/* If the rest of the string still contains the word "Header" | |
* (or any word for that matter), | |
* it won't be reverted to the "Invalid" form | |
*/ | |
let hasWords = split(subStem, " ") | |
.map(s => search(s, regex.letters)) | |
if (hasWords.reduce((a, b) => a && b) === -1) { | |
return `${revert.join("")}Invalid${subStem}` | |
} | |
} | |
// Count the ones, join the string together | |
const count = ones.reduce((a, b) => +a + +b); | |
const body = noOnes.join(""); | |
// Generate the HTML tags | |
const { open, | |
close } = html(count).h; | |
// Return the converted string | |
return `${open}${body}${close}` | |
} | |
console.log(markdownParser('### Header')) | |
// "<h3> Header</h3>" | |
console.log(markdownParser('## ### Header')) | |
// "##Invalid ### Header" | |
console.log(markdownParser('###Header ##')) | |
// "<h3>Header ##</h3>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment