From 6f0a44311410c10d8a31fab2b6dd90a20a20fe52 Mon Sep 17 00:00:00 2001 From: HerrHase Date: Wed, 22 Mar 2023 14:14:38 +0100 Subject: [PATCH] adding --- README.md | 25 ++++++++++++++++++++- package.json | 2 +- ressources/index.md | 4 +++- src/config.js | 7 +----- src/factories/block.js | 2 +- src/factories/page.js | 12 ++++++++--- src/marked/renderer.js | 48 ++++++++++++++++++++++++++++++++++++++++- src/parsers/markdown.js | 12 ++++++++++- test/pages.js | 4 ++++ 9 files changed, 101 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ab38c19..abea463 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,6 @@ finished, it was only a proof of concept. But now it works for created a entire Next will be, * Some more Tests -* Image Handling in Markdown Maybe later, @@ -200,6 +199,30 @@ media: --- ``` +Options from Sharp can be add on two different ways. As "options" for all Sizes or +you can simply add options to a sizes, that means the main options will be ignored. + +``` +--- +title: "health goth DIY tattooed" +view: "home.njk" +meta: + description: "La" +media: + teaser: + src: + src: '_images/dog.jpg' + sizes: + - width: 300 + - width: 500 + height: 100 + position: 'left' + alt: "cold-pressed" + options: + position: 'right' +--- +``` + ## Queries Queries can be used in Templates to get Pages. diff --git a/package.json b/package.json index 78b92f2..2061727 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@site-o-mat/core", - "version": "0.3.2", + "version": "0.4.0", "build": "webpack", "author": "Björn Hase ", "main": "index.js", diff --git a/ressources/index.md b/ressources/index.md index d4def82..ca60532 100644 --- a/ressources/index.md +++ b/ressources/index.md @@ -22,4 +22,6 @@ bicycle rights sartorial godard slow-carb thundercats art party cray JOMO. Truff ## Bitters kale chips chambray activated charcoal -wolf keffiyeh hell of selfies. Wolf readymade shoreditch flexitarian venmo single-origin coffee, knausgaard fit actually street art cold-pressed iPhone gatekeep. Migas bruh adaptogen semiotics marfa pickled yuccie. Locavore normcore lomo, shoreditch fashion axe actually glossier iPhone photo booth blue bottle DIY XOXO williamsburg. Pinterest whatever taxidermy, kale chips prism XOXO schlitz twee tote bag woke swag. Wayfarers fashion axe heirloom humblebrag synth. Whatever succulents PBR&B, pop-up enamel pin echo park tonx stumptown taiyaki. +wolf keffiyeh hell of selfies. Wolf readymade shoreditch flexitarian venmo single-origin coffee, knausgaard fit actually street art cold-pressed iPhone gatekeep. Migas bruh adaptogen semiotics marfa pickled yuccie. Locavore normcore lomo, shoreditch fashion axe actually glossier iPhone photo booth blue bottle DIY XOXO williamsburg. Pinterest whatever taxidermy, kale chips prism XOXO schlitz twee tote bag woke swag. Wayfarers fashion axe heirloom humblebrag synth. Whatever succulents PBR&B, pop-up enamel pin echo park tonx stumptown taiyaki. + +![_images/dog.jpg](_images/dog.jpg) diff --git a/src/config.js b/src/config.js index ef28012..4bfb298 100644 --- a/src/config.js +++ b/src/config.js @@ -37,11 +37,6 @@ class ConfigStore { * @return {String|Object} */ get(key) { - - if (!this._data?.[key]) { - throw new Error(key + ' not found in ConfigStore!') - } - return this._data[key] } } @@ -49,4 +44,4 @@ class ConfigStore { // create instance const instance = new ConfigStore(); -module.exports = instance \ No newline at end of file +module.exports = instance diff --git a/src/factories/block.js b/src/factories/block.js index a784ac2..dd90f63 100644 --- a/src/factories/block.js +++ b/src/factories/block.js @@ -25,7 +25,7 @@ class Block { constructor(fileString, dirPath) { // parse string of file - const parsedFile = parseMarkdownFile(fileString) + const parsedFile = parseMarkdownFile(fileString, dirPath) this._dirPath = dirPath diff --git a/src/factories/page.js b/src/factories/page.js index e8b1c09..91ccd55 100644 --- a/src/factories/page.js +++ b/src/factories/page.js @@ -30,8 +30,11 @@ class Page { */ constructor(file, parent, fileString, blocks = {}) { + // getting dirPath for files for page + this._dirPath = this._resolvePath(parent) + // parse file - const result = parseMarkdownFile(fileString) + const result = parseMarkdownFile(fileString, this._dirPath) // fields merge by default values this._fields = merge({ @@ -46,7 +49,6 @@ class Page { // adding filename for html as pathname and relative path in structure this._filename = this._resolveFilename(file) this._slug = this._resolveSlug(this._filename) - this._dirPath = this._resolvePath(parent) this._permalink = this._dirPath if (this._slug) { @@ -121,7 +123,11 @@ class Page { } if (typeof field === 'object' || field instanceof Object) { - field = media.resolve(field.src, field.sizes) + if (field.options) { + field = media.resolve(field.src, field.sizes, field.options) + } else { + field = media.resolve(field.src, field.sizes) + } } return field diff --git a/src/marked/renderer.js b/src/marked/renderer.js index 3716c09..8c1b18b 100644 --- a/src/marked/renderer.js +++ b/src/marked/renderer.js @@ -1,7 +1,10 @@ const { marked } = require('marked') +const configStore = require('./../config.js') +const Media = require('./../factories/media.js') + /** - * + * * */ @@ -38,6 +41,13 @@ function cleanUrl(sanitize, base, href) { } const renderer = { + + /** + * + * @param {string} href + * @param {string} title + * @param {string} text + */ link(href, title, text) { href = cleanUrl(this.options.sanitize, this.options.baseUrl, href) @@ -59,6 +69,42 @@ const renderer = { out += '>' + text + '' + return out + }, + + /** + * + * @param {string} href + * @param {string} title + * @param {string} text + */ + image(href, title, text) { + + href = cleanUrl(this.options.sanitize, this.options.baseUrl, href) + + if (href === null) { + return text + } + + // check if href for image is relative + if (!href.match(/^(http|https):\/\//)) { + const markedDirPath = configStore.get('markedDirPath') + + // check if dirPath are exists from options + if (markedDirPath || markedDirPath === '') { + const media = new Media(markedDirPath) + href = media.resolve(href) + } + } + + let out = `${text}' : '>' + return out } } diff --git a/src/parsers/markdown.js b/src/parsers/markdown.js index 40ade1d..57c238b 100644 --- a/src/parsers/markdown.js +++ b/src/parsers/markdown.js @@ -1,6 +1,7 @@ const yaml = require('js-yaml') const { marked } = require('marked') +const configStore = require('./../config.js') const renderer = require('./../marked/renderer.js') /** @@ -12,7 +13,7 @@ const renderer = require('./../marked/renderer.js') * */ -function parseMarkdownFile(fileString) { +function parseMarkdownFile(fileString, dirPath = '') { // regex get yaml section and markdown // thanks to, https://github.com/getgrav/grav @@ -35,6 +36,15 @@ function parseMarkdownFile(fileString) { // if markdown section exits parse it to html 6565 if (matches?.[3]) { + + // reset configStore + configStore.set('markedDirPath', false) + + // check for dirPath and set it to configStore for marked/renderer.js + if (dirPath || dirPath === '') { + configStore.set('markedDirPath', dirPath) + } + marked.use({ renderer }) result.content = marked.parse(matches[3]) } diff --git a/test/pages.js b/test/pages.js index 7a73390..50ba339 100644 --- a/test/pages.js +++ b/test/pages.js @@ -29,6 +29,10 @@ describe('Page /index.md', function () { it('permalink', function() { assert.equal(page.permalink, '') }) + + it('parsed content contains image', () => { + assert.match(page.content, /_images\/dog.jpg/) + }) }) describe('Page /blog/index.md', function () {