mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-11-16 07:58:44 +00:00
Update Download endpoint to export four shapes of Markdown files
Restructures PDF download into an Export menu containing the PDF export and 4 Markdown Options Slightly tweaks tests Effectively moves the api.mergeBrewText to helpers. I think this makes more sense. Changes the download extension from txt to 'md`. The Commonmark, TOML, and JSON exports are the same with the frontmatter in a slightly different shape. These were selected based on lightweight research into Markdown Frontmatter. If specific formats - such as Obsidian - are different enough in terms of formatting the frontmatter or desired metadata content, this is fairly easy
This commit is contained in:
+2
-24
@@ -10,7 +10,7 @@ import asyncHandler from 'express-async-handler';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { makePatches, applyPatches, stringifyPatches, parsePatch } from '@sanity/diff-match-patch';
|
||||
import { md5 } from 'hash-wasm';
|
||||
import { splitTextStyleAndMetadata,
|
||||
import { splitTextStyleAndMetadata, transformFrontmatter,
|
||||
brewSnippetsToJSON, debugTextMismatch } from '../shared/helpers.js';
|
||||
import checkClientVersion from './middleware/check-client-version.js';
|
||||
import dbCheck from './middleware/dbCheck.js';
|
||||
@@ -209,29 +209,7 @@ const api = {
|
||||
return res.status(200).send(brew.style);
|
||||
},
|
||||
mergeBrewText : (brew)=>{
|
||||
let text = brew.text;
|
||||
if(brew.style !== undefined) {
|
||||
text = `\`\`\`css\n` +
|
||||
`${brew.style || ''}\n` +
|
||||
`\`\`\`\n\n` +
|
||||
`${text}`;
|
||||
}
|
||||
const metadata = _.pick(brew, ['title', 'description', 'tags', 'renderer', 'theme']);
|
||||
const snippetsArray = brewSnippetsToJSON('brew_snippets', brew.snippets, null, false).snippets;
|
||||
metadata.snippets = snippetsArray.length > 0 ? snippetsArray : undefined;
|
||||
metadata.bleedSize = { top: brew?.bleedSize?.top, bottom: brew?.bleedSize?.bottom, inner: brew?.bleedSize?.inner, outer: brew?.bleedSize?.outer };
|
||||
metadata.safetySpace = { top: brew?.safetySpace?.top, bottom: brew?.safetySpace?.bottom, outer: brew?.safetySpace?.outer, inner: brew?.safetySpace?.inner };
|
||||
metadata.trimSize = { width: brew?.trimSize?.width, height: brew?.trimSize?.height };
|
||||
metadata.columns = brew?.columns;
|
||||
metadata.columnGutter = brew?.columnGutter;
|
||||
metadata.license = brew?.license;
|
||||
metadata.legalAuthors = brew?.legalAuthors;
|
||||
|
||||
text = `\`\`\`metadata\n` +
|
||||
`${yaml.dump(metadata)}\n` +
|
||||
`\`\`\`\n\n` +
|
||||
`${text}`;
|
||||
return text;
|
||||
return transformFrontmatter(brew);
|
||||
},
|
||||
getGoodBrewTitle : (text)=>{
|
||||
const tokens = hbfm.marked.lexer(text);
|
||||
|
||||
@@ -499,8 +499,6 @@ description: this is a description
|
||||
tags:
|
||||
- something
|
||||
- fun
|
||||
renderer: v3
|
||||
theme: phb
|
||||
bleedSize:
|
||||
top: .125in
|
||||
bottom: .125in
|
||||
@@ -518,6 +516,8 @@ columns: '2'
|
||||
columnGutter: .125in
|
||||
license: None
|
||||
legalAuthors: ''
|
||||
renderer: v3
|
||||
theme: phb
|
||||
|
||||
\`\`\`
|
||||
|
||||
@@ -554,7 +554,7 @@ brew`);
|
||||
width : '8.5in',
|
||||
height : '11in',
|
||||
},
|
||||
});
|
||||
}, 'hb');
|
||||
|
||||
expect(result).toEqual(`\`\`\`metadata
|
||||
title: some title
|
||||
@@ -562,8 +562,6 @@ description: this is a description
|
||||
tags:
|
||||
- something
|
||||
- fun
|
||||
renderer: v3
|
||||
theme: phb
|
||||
bleedSize:
|
||||
top: .125in
|
||||
bottom: .125in
|
||||
@@ -581,6 +579,8 @@ columns: '2'
|
||||
columnGutter: .125in
|
||||
license: None
|
||||
legalAuthors: ''
|
||||
renderer: v3
|
||||
theme: phb
|
||||
|
||||
\`\`\`
|
||||
|
||||
|
||||
+13
-3
@@ -15,7 +15,7 @@ import fs from 'fs';
|
||||
import dbCheck from './middleware/dbCheck.js';
|
||||
import sanitizeFilename from 'sanitize-filename';
|
||||
import { DEFAULT_BREW } from './brewDefaults.js';
|
||||
import { splitTextStyleAndMetadata } from '../shared/helpers.js';
|
||||
import { splitTextStyleAndMetadata, transformFrontmatter } from '../shared/helpers.js';
|
||||
import GoogleActions from './googleActions.js';
|
||||
|
||||
import api from './homebrew.api.js';
|
||||
@@ -135,9 +135,19 @@ export default function pageRoutes({
|
||||
});
|
||||
|
||||
//Download brew source page
|
||||
app.get('/download/:id', asyncHandler(getBrew('share')), (req, res)=>{
|
||||
app.get('/download/:format/:id', asyncHandler(getBrew('share')), async (req, res)=>{
|
||||
const markdownFormats = {
|
||||
hb : 'homebrewery',
|
||||
cm : 'commonmark',
|
||||
toml : 'toml',
|
||||
json : 'json'
|
||||
};
|
||||
|
||||
const { brew } = req;
|
||||
|
||||
const lastBrew = brew.text;
|
||||
sanitizeBrew(brew, 'share');
|
||||
brew.text = await transformFrontmatter(brew, req.params.format);
|
||||
const prefix = 'HB - ';
|
||||
|
||||
const encodeRFC3986ValueChars = (str)=>{
|
||||
@@ -152,7 +162,7 @@ export default function pageRoutes({
|
||||
res.set({
|
||||
'Cache-Control' : 'no-cache',
|
||||
'Content-Type' : 'text/plain',
|
||||
'Content-Disposition' : `attachment; filename*=UTF-8''${encodeRFC3986ValueChars(fileName)}.txt`
|
||||
'Content-Disposition' : `attachment; filename*=UTF-8''${encodeRFC3986ValueChars(fileName)}.${markdownFormats[req.params.format]}.md`
|
||||
});
|
||||
res.status(200).send(brew.text);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user