mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-24 22:52:40 +00:00
Convert storage of snippets in Brew to yaml by request.
This commit is contained in:
@@ -8,9 +8,11 @@ import Markdown from '../shared/naturalcrit/markdown.js';
|
||||
import yaml from 'js-yaml';
|
||||
import asyncHandler from 'express-async-handler';
|
||||
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';
|
||||
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
import { DEFAULT_BREW, DEFAULT_BREW_LOAD } from './brewDefaults.js';
|
||||
@@ -176,15 +178,15 @@ const api = {
|
||||
|
||||
mergeBrewText : (brew)=>{
|
||||
let text = brew.text;
|
||||
if(brew.style !== undefined) {
|
||||
text = `\`\`\`css\n` +
|
||||
`${brew.style || ''}\n` +
|
||||
if(brew.snippets !== undefined) {
|
||||
text = `\`\`\`snippets\n` +
|
||||
`${yaml.dump(brewSnippetsToJSON('brew_snippets', brew.snippets, null, false))}` +
|
||||
`\`\`\`\n\n` +
|
||||
`${text}`;
|
||||
}
|
||||
if(brew.snippets !== undefined) {
|
||||
text = `\`\`\`snippets\n` +
|
||||
`${brew.snippets || ''}\n` +
|
||||
if(brew.style !== undefined) {
|
||||
text = `\`\`\`css\n` +
|
||||
`${brew.style || ''}\n` +
|
||||
`\`\`\`\n\n` +
|
||||
`${text}`;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import yaml from 'js-yaml';
|
||||
import request from '../client/homebrew/utils/request-middleware.js';
|
||||
|
||||
// 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 mpAsSnippets = [];
|
||||
// Snippets from Themes first.
|
||||
@@ -17,7 +17,7 @@ const brewSnippetsToJSON = (menuTitle, userBrewSnippets, themeBundleSnippets)=>{
|
||||
userSnippets.push({
|
||||
name : name.slice('\snippets'.length),
|
||||
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 = [];
|
||||
for (let snips of userBrewSnippets.trim().split(textSplit)) {
|
||||
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) {
|
||||
userSnippets.push({
|
||||
const subSnip = {
|
||||
name : name,
|
||||
icon : '',
|
||||
gen : snips.slice(name.length + 1),
|
||||
});
|
||||
gen : justSnippet,
|
||||
};
|
||||
// if(full) subSnip.icon = '';
|
||||
userSnippets.push(subSnip);
|
||||
}
|
||||
}
|
||||
if(userSnippets.length) {
|
||||
mpAsSnippets.push({
|
||||
name : menuTitle,
|
||||
icon : '',
|
||||
// icon : '',
|
||||
subsnippets : userSnippets
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
groupName : 'Brew Snippets',
|
||||
icon : 'fas fa-th-list',
|
||||
view : 'text',
|
||||
snippets : mpAsSnippets
|
||||
const returnObj = {
|
||||
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)=>{
|
||||
brew.text = brew.text.replaceAll('\r\n', '\n');
|
||||
if(brew.text.startsWith('```metadata')) {
|
||||
const index = brew.text.indexOf('```\n\n');
|
||||
const metadataSection = brew.text.slice(12, index - 1);
|
||||
const index = brew.text.indexOf('\n```\n\n');
|
||||
const metadataSection = brew.text.slice(11, index - 1);
|
||||
const metadata = yaml.load(metadataSection);
|
||||
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')) {
|
||||
const index = brew.text.indexOf('```\n\n');
|
||||
brew.style = brew.text.slice(7, index - 1);
|
||||
brew.text = brew.text.slice(index + 5);
|
||||
const index = brew.text.indexOf('\n```\n\n');
|
||||
brew.style = brew.text.slice(6, index - 1);
|
||||
brew.text = brew.text.slice(index + 6);
|
||||
}
|
||||
if(brew.text.startsWith('```snippets')) {
|
||||
const index = brew.text.indexOf('```\n\n');
|
||||
brew.snippets = brew.text.slice(12, index - 1);
|
||||
brew.text = brew.text.slice(index + 5);
|
||||
const index = brew.text.indexOf('\n```\n\n');
|
||||
brew.snippets = yamlSnippetsToText(yaml.load(brew.text.slice(11, index - 1))).slice(0, -1);
|
||||
brew.text = brew.text.slice(index + 6);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user