<commit_before>import fs from 'fs'
import yaml from 'js-yaml'
import renderMarkdown from '@/utils/renderMarkdown.js'
import renderPostBody from '@/utils/renderPostBody.js'
import { findInManifest } from '@/utils/imageHelpers.js'

export function get(req, res, next) {
	const { year, month, day, slug } = req.params
	const header = {
		'Content-Type': 'application/json'
	}

	let data = yaml.safeLoad(
		fs.readFileSync(
			`content/writing/${year}-${month}-${day}-${slug}.yml`,
			'utf-8'
		)
	)

	if (!data) {
		res.writeHead(404, header)
		res.end(JSON.stringify({
			message: 'Oh dear, this bicycle has gone rubber-side-up. Much appreciated if you could let <a href="https://twitter.com/inkpixelswords" target="_blank">@inkpixelswords</a> know.'
		}))
	}

	if (data.cover) {

		// resize the cover image if needed
		if (data.cover.resize) {
			data.cover.image = findInManifest(data.cover.image)
		}

		// render the cover image caption if present
		if (data.cover.caption) {
			data.cover.caption = renderMarkdown(data.cover.caption, {
				inline: true,
				html: true
			})
		}

		// render the cover image attribution if present
		if (data.cover.credit) {
			data.cover.credit = renderMarkdown(data.cover.credit, {
				inline: true,
				html: true
			})
		}
	}

	// render markdown as needed
	data.body = renderPostBody(data.body)

	// resize images as needed
	data.body.forEach((block) => {
		// if it has an 'image' field (e.g. figure), resize it
		if (block.image && block.resize) {
			block.image = findInManifest(block.image)
		}

		if (block.images) {
			// if it has an 'images' field (e.g. gallery), resize each
			block.images.forEach(async (item) => {
				if (item.resize) {
					item.image = findInManifest(item.image)
				}
			})
		}
	})

	// create responsive resizes of images as needed

	res.writeHead(200, header)
	res.end(JSON.stringify(data))
}<commit_msg>[#87] fix glitch in gallery resizing<commit_after>import fs from 'fs'
import yaml from 'js-yaml'
import renderMarkdown from '@/utils/renderMarkdown.js'
import renderPostBody from '@/utils/renderPostBody.js'
import { findInManifest } from '@/utils/imageHelpers.js'

export function get(req, res, next) {
	const { year, month, day, slug } = req.params
	const header = {
		'Content-Type': 'application/json'
	}

	let data = yaml.safeLoad(
		fs.readFileSync(
			`content/writing/${year}-${month}-${day}-${slug}.yml`,
			'utf-8'
		)
	)

	if (!data) {
		res.writeHead(404, header)
		res.end(JSON.stringify({
			message: 'Oh dear, this bicycle has gone rubber-side-up. Much appreciated if you could let <a href="https://twitter.com/inkpixelswords" target="_blank">@inkpixelswords</a> know.'
		}))
	}

	if (data.cover) {

		// resize the cover image if needed
		if (data.cover.resize) {
			data.cover.image = findInManifest(data.cover.image)
		}

		// render the cover image caption if present
		if (data.cover.caption) {
			data.cover.caption = renderMarkdown(data.cover.caption, {
				inline: true,
				html: true
			})
		}

		// render the cover image attribution if present
		if (data.cover.credit) {
			data.cover.credit = renderMarkdown(data.cover.credit, {
				inline: true,
				html: true
			})
		}
	}

	// render markdown as needed
	data.body = renderPostBody(data.body)

	// resize images as needed
	data.body.forEach((block) => {
		if (block.resize) {
			// if it has an 'image' field (e.g. figure), resize it
			if (block.image) {
				block.image = findInManifest(block.image)
			}

			if (block.images) {
				// if it has an 'images' field (e.g. gallery), resize each
				block.images.forEach((item) => {
					item.image = findInManifest(item.image)
				})
			}
		}
	})

	// create responsive resizes of images as needed

	res.writeHead(200, header)
	res.end(JSON.stringify(data))
}
{'fraction_non_alphanumeric': 0.14494292540483142, 'fraction_numerical': 0.004247411733474914, 'mean_word_length': 3.2672706681766703, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7055456', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1282, 'n_words': 323}