0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-05 05:52:46 +00:00

Convert storage of snippets in Brew to yaml by request.

This commit is contained in:
David Bolack
2024-11-24 18:13:32 -06:00
parent c5935ec262
commit b6e445c445
2 changed files with 52 additions and 28 deletions

View File

@@ -8,9 +8,11 @@ import Markdown from '../shared/naturalcrit/markdown.js';
import yaml from 'js-yaml'; import yaml from 'js-yaml';
import asyncHandler from 'express-async-handler'; import asyncHandler from 'express-async-handler';
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
import { splitTextStyleAndMetadata } from '../shared/helpers.js'; import { splitTextStyleAndMetadata,
brewSnippetsToJSON } from '../shared/helpers.js';
import checkClientVersion from './middleware/check-client-version.js'; import checkClientVersion from './middleware/check-client-version.js';
const router = express.Router(); const router = express.Router();
import { DEFAULT_BREW, DEFAULT_BREW_LOAD } from './brewDefaults.js'; import { DEFAULT_BREW, DEFAULT_BREW_LOAD } from './brewDefaults.js';
@@ -176,15 +178,15 @@ const api = {
mergeBrewText : (brew)=>{ mergeBrewText : (brew)=>{
let text = brew.text; let text = brew.text;
if(brew.style !== undefined) { if(brew.snippets !== undefined) {
text = `\`\`\`css\n` + text = `\`\`\`snippets\n` +
`${brew.style || ''}\n` + `${yaml.dump(brewSnippetsToJSON('brew_snippets', brew.snippets, null, false))}` +
`\`\`\`\n\n` + `\`\`\`\n\n` +
`${text}`; `${text}`;
} }
if(brew.snippets !== undefined) { if(brew.style !== undefined) {
text = `\`\`\`snippets\n` + text = `\`\`\`css\n` +
`${brew.snippets || ''}\n` + `${brew.style || ''}\n` +
`\`\`\`\n\n` + `\`\`\`\n\n` +
`${text}`; `${text}`;
} }

View File

@@ -3,7 +3,7 @@ import yaml from 'js-yaml';
import request from '../client/homebrew/utils/request-middleware.js'; import request from '../client/homebrew/utils/request-middleware.js';
// Convert the templates from a brew to a Snippets Structure. // Convert the templates from a brew to a Snippets Structure.
const brewSnippetsToJSON = (menuTitle, userBrewSnippets, themeBundleSnippets)=>{ const brewSnippetsToJSON = (menuTitle, userBrewSnippets, themeBundleSnippets=null, full=true)=>{
const textSplit = /^\\snippet /gm; const textSplit = /^\\snippet /gm;
const mpAsSnippets = []; const mpAsSnippets = [];
// Snippets from Themes first. // Snippets from Themes first.
@@ -17,7 +17,7 @@ const brewSnippetsToJSON = (menuTitle, userBrewSnippets, themeBundleSnippets)=>{
userSnippets.push({ userSnippets.push({
name : name.slice('\snippets'.length), name : name.slice('\snippets'.length),
icon : '', icon : '',
gen : snips.slice(name.length + 1), gen : snips.slice(name.length + 1).trim(),
}); });
} }
} }
@@ -37,49 +37,71 @@ const brewSnippetsToJSON = (menuTitle, userBrewSnippets, themeBundleSnippets)=>{
const userSnippets = []; const userSnippets = [];
for (let snips of userBrewSnippets.trim().split(textSplit)) { for (let snips of userBrewSnippets.trim().split(textSplit)) {
let name = snips.split('\n')[0]; let name = snips.split('\n')[0];
let justSnippet = snips.slice(name.length + 1);
if(justSnippet.slice(-1) === '\n') {
justSnippet = justSnippet.slice(0, -1);
}
if(name.length != 0) { if(name.length != 0) {
userSnippets.push({ const subSnip = {
name : name, name : name,
icon : '', gen : justSnippet,
gen : snips.slice(name.length + 1), };
}); // if(full) subSnip.icon = '';
userSnippets.push(subSnip);
} }
} }
if(userSnippets.length) { if(userSnippets.length) {
mpAsSnippets.push({ mpAsSnippets.push({
name : menuTitle, name : menuTitle,
icon : '', // icon : '',
subsnippets : userSnippets subsnippets : userSnippets
}); });
} }
} }
return { const returnObj = {
groupName : 'Brew Snippets', snippets : mpAsSnippets
icon : 'fas fa-th-list',
view : 'text',
snippets : mpAsSnippets
}; };
if(full) {
returnObj.groupName = 'Brew Snippets';
returnObj.icon = 'fas fa-th-list';
returnObj.view = 'text';
}
return returnObj;
};
const yamlSnippetsToText = (yamlObj)=>{
if(typeof yamlObj == 'string') return yamlObj;
let snippetsText = '';
for (let snippet of yamlObj.snippets) {
for (let subSnippet of snippet.subsnippets) {
snippetsText = `${snippetsText}\\snippet ${subSnippet.name}\n${subSnippet.gen || ''}\n`;
}
}
return snippetsText;
}; };
const splitTextStyleAndMetadata = (brew)=>{ const splitTextStyleAndMetadata = (brew)=>{
brew.text = brew.text.replaceAll('\r\n', '\n'); brew.text = brew.text.replaceAll('\r\n', '\n');
if(brew.text.startsWith('```metadata')) { if(brew.text.startsWith('```metadata')) {
const index = brew.text.indexOf('```\n\n'); const index = brew.text.indexOf('\n```\n\n');
const metadataSection = brew.text.slice(12, index - 1); const metadataSection = brew.text.slice(11, index - 1);
const metadata = yaml.load(metadataSection); const metadata = yaml.load(metadataSection);
Object.assign(brew, _.pick(metadata, ['title', 'description', 'tags', 'systems', 'renderer', 'theme', 'lang'])); Object.assign(brew, _.pick(metadata, ['title', 'description', 'tags', 'systems', 'renderer', 'theme', 'lang']));
brew.text = brew.text.slice(index + 5); brew.text = brew.text.slice(index + 6);
} }
if(brew.text.startsWith('```css')) { if(brew.text.startsWith('```css')) {
const index = brew.text.indexOf('```\n\n'); const index = brew.text.indexOf('\n```\n\n');
brew.style = brew.text.slice(7, index - 1); brew.style = brew.text.slice(6, index - 1);
brew.text = brew.text.slice(index + 5); brew.text = brew.text.slice(index + 6);
} }
if(brew.text.startsWith('```snippets')) { if(brew.text.startsWith('```snippets')) {
const index = brew.text.indexOf('```\n\n'); const index = brew.text.indexOf('\n```\n\n');
brew.snippets = brew.text.slice(12, index - 1); brew.snippets = yamlSnippetsToText(yaml.load(brew.text.slice(11, index - 1))).slice(0, -1);
brew.text = brew.text.slice(index + 5); brew.text = brew.text.slice(index + 6);
} }
}; };