From 2f8ccc7ad15ce61d1c954007f271242061d4398c Mon Sep 17 00:00:00 2001 From: wholteza Date: Sun, 17 Sep 2023 10:18:10 +0200 Subject: [PATCH] WIP Links --- markdown.js | 141 +++++++++++++++++++++++++++++++++ notes/1-initial-commit/note.md | 2 + 2 files changed, 143 insertions(+) diff --git a/markdown.js b/markdown.js index bb742ea..2d5275c 100644 --- a/markdown.js +++ b/markdown.js @@ -99,6 +99,124 @@ class Heading extends Symbol { } } +class UnorderedListItem extends Symbol { + /** + * @type {string} + */ + text = ""; + /** + * @type {number} + */ + level = 0; + /** + * + * @param {string} line + * @returns {boolean} + */ + static canParse(line) { + return line.trim().startsWith("- "); + } + + /** + * + * @param {LineFeed} lineFeed + * @returns {Symbol} + */ + static create(lineFeed) { + const instance = new UnorderedListItem(); + const line = lineFeed.claim(); + instance.text = line.replaceAll("-", "").trim(); + instance.level = getAmountOfTokenInBeginningOfFile(" ", line); + return instance; + } + + render() { + return `
  • ${this.text} indentation level ${this.level}
  • `; + } +} + +class OrderedListItem extends Symbol { + /** + * @type {string} + */ + text = ""; + /** + * @type {number} + */ + level = 0; + /** + * + * @param {string} line + * @returns {boolean} + */ + static canParse(line) { + return new RegExp(/^\d\. /).test(line.trim()); + } + + /** + * + * @param {LineFeed} lineFeed + * @returns {Symbol} + */ + static create(lineFeed) { + const instance = new UnorderedListItem(); + const line = lineFeed.claim(); + instance.text = line.trim().replace(/^\d\. /, ""); + instance.level = getAmountOfTokenInBeginningOfFile(" ", line); + return instance; + } + + render() { + return `
  • ${this.text} indentation level ${this.level}
  • `; + } +} + +class Link extends Symbol { + /** + * @type {RegExp} + */ + static canParseRegExp = new RegExp(/^\[.*\]\(.*\)/); + /** + * @type {RegExp} + */ + static textAndLinkRegExp = new RegExp(/\[(?.*)\]\((?.*)\)/); + /** + * @type {string} + */ + text = ""; + /** + * @type {string} + */ + link = ""; + /** + * + * @param {string} line + * @returns {boolean} + */ + static canParse(line) { + return Link.canParseRegExp.test(line.trim()); + } + + /** + * + * @param {LineFeed} lineFeed + * @returns {Symbol} + */ + static create(lineFeed) { + const instance = new Link(); + const line = lineFeed.claim().trim(); + const { text, link } = Link.textAndLinkRegExp.exec(line)?.groups ?? {}; + console.log("test:", text, link); + instance.link = link ?? ""; + instance.text = text ?? ""; + return instance; + } + + render() { + return `${this.text}`; + } +} + class Italic extends Symbol { /** * @type {string} @@ -350,10 +468,33 @@ const splitStringAtToken = (token, string) => { return [string.slice(0, index), string.slice(index + token.length)]; }; +/** + * + * @param {string} token + * @param {string} string + */ +const getAmountOfTokenInBeginningOfFile = (token, string) => { + let count = 0; + let searchIndex = 0; + while (true) { + if (searchIndex + (token.length - 1) > string.length) return count; + + const index = string.slice(searchIndex).indexOf(token); + const indexNotAtStartOfString = index !== 0; + if (indexNotAtStartOfString) return count; + + count++; + searchIndex += token.length; + } +}; + const Factories = [ Heading, + UnorderedListItem, + OrderedListItem, Bold, Italic, + Link, MultiLineCode, SingleLineCode, CatchAll, diff --git a/notes/1-initial-commit/note.md b/notes/1-initial-commit/note.md index 27ddfed..f7ea6dc 100644 --- a/notes/1-initial-commit/note.md +++ b/notes/1-initial-commit/note.md @@ -62,10 +62,12 @@ const y = () => { - bullet - bullet + - bullet - bullet 1. ordered list item 1. ordered list item + 1. ordered list item 1. ordered list item [link to the index](https://blog.zacke.dev)