Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions client/components/codeEditor/codeEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import {
crosshairCursor,
} from '@codemirror/view';
import { EditorState, Compartment, StateEffect, StateField } from '@codemirror/state';
import { foldAll as foldAllCmd, unfoldAll as unfoldAllCmd, foldGutter, foldKeymap, syntaxHighlighting } from '@codemirror/language';
import { foldEffect } from '@codemirror/language';
import { foldAll as foldAllCmd, unfoldAll as unfoldAllCmd, foldGutter, foldKeymap, foldEffect, syntaxHighlighting } from '@codemirror/language';
import { defaultKeymap, history, undo, redo, undoDepth, redoDepth } from '@codemirror/commands';
import { languages } from '@codemirror/language-data';
import { css } from '@codemirror/lang-css';
Expand All @@ -38,14 +37,13 @@ const themes = { default: defaultCM5Theme, ...cm5Themes, darkbrewery };
const themeCompartment = new Compartment();
const highlightCompartment = new Compartment();

console.log(themes);

import { generalKeymap, markdownKeymap } from './customKeyMaps.js';
import foldOnPages from './customFolding.js';
import { customHighlightStyle, tokenizeCustomMarkdown, tokenizeCustomCSS } from './customHighlight.js';
import { legacyCustomHighlightStyle, legacyTokenizeCustomMarkdown } from './legacyCustomHighlight.js';
import { regexList } from '@shared/helpers.js';

const PAGEBREAK_REGEX_V3 = /^(?=\\page(?:break)?(?: *{[^\n{}]*})?$)/m;
const PAGEBREAK_REGEX_V3 = regexList.v3.pageBreak;

const createHighlightPlugin = (renderer, tab)=>{
//this function takes the custom tokens created in the tokenize function in customhighlight files
Expand Down Expand Up @@ -156,6 +154,7 @@ const CodeEditor = forwardRef(
const pageMap = useRef([]);

const recomputePages = (doc)=>{
if (tab !== 'brewText') return;
const pages = [0];
const text = doc.toString();
let offset = 0;
Expand Down
7 changes: 3 additions & 4 deletions client/components/codeEditor/customFolding.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { foldService, codeFolding } from '@codemirror/language';
import { regexList } from '@shared/helpers.js';

const foldOnPages = [
foldService.of((state, lineStart)=>{ //tells where to fold
const doc = state.doc;
const matcher = /^(?=\\page(?:break)?(?: *{[^\n{}]*})?$)/m;

const startLine = doc.lineAt(lineStart);
const prevLineText = startLine.number > 1 ? doc.line(startLine.number - 1).text : '';

if(!matcher.test(prevLineText)) return null;
if(!regexList.v3.pageBreak.test(prevLineText)) return null;

let endLine = startLine.number;
while (endLine < doc.lines && !matcher.test(doc.line(endLine + 1).text)) {
while (endLine < doc.lines && !regexList.v3.pageBreak.test(doc.line(endLine + 1).text)) {
endLine++;
}

Expand Down
115 changes: 52 additions & 63 deletions client/components/codeEditor/customHighlight.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HighlightStyle } from '@codemirror/language';
import { tags } from '@lezer/highlight';
import { regexList } from '@shared/helpers.js';

// Making the tokens
const customTags = {
Expand Down Expand Up @@ -30,15 +31,14 @@ export function tokenizeCustomMarkdown(text) {

lines.forEach((lineText, lineNumber)=>{
// --- Page / snippet lines ---
if(/^(?=\\page(?:break)?(?: *{[^\n{}]*})?$)/m.test(lineText)) tokens.push({ line: lineNumber, type: customTags.pageLine });
if(/^\\snippet\ .*$/.test(lineText)) tokens.push({ line: lineNumber, type: customTags.snippetLine });
if(/^\\column(?:break)?$/.test(lineText)) tokens.push({ line: lineNumber, type: customTags.columnSplit });
if(regexList.v3.pageBreak.test(lineText)) tokens.push({ line: lineNumber, type: customTags.pageLine });
if(regexList.v3.snippetBreak.test(lineText)) tokens.push({ line: lineNumber, type: customTags.snippetLine });
if(regexList.v3.columnBreak.test(lineText)) tokens.push({ line: lineNumber, type: customTags.columnSplit });

// --- Emoji ---
if(/:.\w+?:/.test(lineText)) {
const emojiRegex = /(:\w+?:)/g;
if(regexList.v3.emoji.test(lineText)) {
let match;
while ((match = emojiRegex.exec(lineText)) !== null) {
while ((match = regexList.v3.emoji.exec(lineText)) !== null) {
tokens.push({
line : lineNumber,
type : customTags.emoji,
Expand All @@ -51,8 +51,8 @@ export function tokenizeCustomMarkdown(text) {
// --- Superscript / Subscript ---
if(/\^/.test(lineText)) {
let startIndex = lineText.indexOf('^');
const superRegex = /\^(?!\s)(?=([^\n\^]*[^\s\^]))\1\^/gy;
const subRegex = /\^\^(?!\s)(?=([^\n\^]*[^\s\^]))\1\^\^/gy;
const superRegex = regexList.v3.superscript;
const subRegex = regexList.v3.subscript;

while (startIndex >= 0) {
superRegex.lastIndex = subRegex.lastIndex = startIndex;
Expand Down Expand Up @@ -82,49 +82,46 @@ export function tokenizeCustomMarkdown(text) {
}

// --- single line def list ---
const singleLineRegex = /^(?=.*[^:])(.+?)(\s*)(::)([^\n]*)$/dmy;
const match = singleLineRegex.exec(lineText);

if(match) {
const [full, term, spaces, colons, desc] = match;

let offset = 0;

tokens.push({
line : lineNumber,
type : customTags.definitionList,
});

// Term
tokens.push({
line : lineNumber,
type : customTags.definitionTerm,
from : offset,
to : offset + term.length,
});
offset += term.length;

// Spaces before ::
if(spaces) {
offset += spaces.length;
}
if(lineText.includes('::')) {
const match = regexList.v3.defListSingleLine.exec(lineText);

if(match) {
const [full, term, spaces, colons, desc] = match;

let offset = 0;

tokens.push({
line : lineNumber,
type : customTags.definitionList,
});

tokens.push({
line : lineNumber,
type : customTags.definitionTerm,
from : offset,
to : offset + term.length,
});
offset += term.length;

// :: colons
tokens.push({
line : lineNumber,
type : customTags.definitionColon,
from : offset,
to : offset + colons.length,
});
offset += colons.length;

// Definition
tokens.push({
line : lineNumber,
type : customTags.definitionDesc,
from : offset,
to : offset + desc.length,
});
if(spaces) {
offset += spaces.length;
}

tokens.push({
line : lineNumber,
type : customTags.definitionColon,
from : offset,
to : offset + colons.length,
});
offset += colons.length;

tokens.push({
line : lineNumber,
type : customTags.definitionDesc,
from : offset,
to : offset + desc.length,
});
}
}

// --- multiline def list ---
Expand Down Expand Up @@ -182,9 +179,8 @@ export function tokenizeCustomMarkdown(text) {
}

if(lineText.includes('{') && lineText.includes('}')) {
const injectionRegex = /(?:^|[^{\n])({(?=((?:[:=](?:"[\w,\-()#%. ]*"|[\w\-()#%.]*)|[^"':={}\s]*)*))\2})/gm;
let match;
while ((match = injectionRegex.exec(lineText)) !== null) {
while ((match = regexList.v3.injection.exec(lineText)) !== null) {
tokens.push({
line : lineNumber,
from : match.index,
Expand All @@ -195,10 +191,9 @@ export function tokenizeCustomMarkdown(text) {
}
if(lineText.includes('{{') && lineText.includes('}}')) {
// Inline blocks: single-line {{…}}
const spanRegex = /{{(?=((?:[:=](?:"[\w,\-()#%. ]*"|[\w\-()#%.]*)|[^"':={}\s]*)*))\1 *|}}/g;
let match;
let blockCount = 0;
while ((match = spanRegex.exec(lineText)) !== null) {
while ((match = regexList.v3.inline_block.exec(lineText)) !== null) {
if(match[0].startsWith('{{')) {
blockCount += 1;
} else {
Expand All @@ -219,9 +214,7 @@ export function tokenizeCustomMarkdown(text) {
// Highlight block divs {{\n Content \n}}
let endCh = lineText.length + 1;

const match = lineText.match(
/^ *{{(?=((?:[:=](?:"[\w,\-()#%. ]*"|[\w\-()#%.]*)|[^"':={}\s]*)*))\1 *$|^ *}}$/,
);
const match = lineText.match(regexList.v3.block);
if(match) endCh = match.index + match[0].length;
tokens.push({ line: lineNumber, type: customTags.block });
}
Expand All @@ -236,10 +229,9 @@ export function tokenizeCustomCSS(text) {

lines.forEach((lineText, lineNumber)=>{

if(/--[a-zA-Z0-9-_]+/gm.test(lineText)) {
const varRegex =/--[a-zA-Z0-9-_]+/gm;
if(regexList.css.variable.test(lineText)) {
let match;
while ((match = varRegex.exec(lineText)) !== null) {
while ((match = regexList.css.variable.exec(lineText)) !== null) {
tokens.push({
line : lineNumber,
from : match.index +1,
Expand Down Expand Up @@ -288,6 +280,3 @@ export const customHighlightStyle = HighlightStyle.define([
{ tag: tags.invalid, class: 'cm-error' },

]);



4 changes: 3 additions & 1 deletion client/homebrew/brewRenderer/brewRenderer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import { printCurrentBrew } from '@shared/helpers.js';
import HeaderNav from './headerNav/headerNav.jsx';
import safeHTML from './safeHTML.js';

const PAGEBREAK_REGEX_V3 = /^(?=\\page(?:break)?(?: *{[^\n{}]*})?$)/m;
import { regexList } from '@shared/helpers.js';

const PAGEBREAK_REGEX_V3 = regexList.v3.pageBreak;
const PAGEBREAK_REGEX_LEGACY = /\\page(?:break)?/m;
const COLUMNBREAK_REGEX_LEGACY = /\\column(:?break)?/m;
const PAGE_HEIGHT = 1056;
Expand Down
3 changes: 0 additions & 3 deletions client/homebrew/editor/editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ const EditorThemes = Object.entries(themes)
)
.map(([name])=>name);


//const PAGEBREAK_REGEX_V3 = /^(?=\\page(?:break)?(?: *{[^\n{}]*})?$)/m;
//const SNIPPETBREAK_REGEX_V3 = /^\\snippet\ .*$/;
const DEFAULT_STYLE_TEXT = dedent`
/*=======--- Example CSS styling ---=======*/
/* Any CSS here will apply to your document! */
Expand Down
3 changes: 2 additions & 1 deletion client/homebrew/pages/editPage/editPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const SAVE_TIMEOUT = 10000;
const UNSAVED_WARNING_TIMEOUT = 900000; //Warn user afer 15 minutes of unsaved changes
const UNSAVED_WARNING_POPUP_TIMEOUT = 4000; //Show the warning for 4 seconds

import { regexList } from '@shared/helpers.js';

const AUTOSAVE_KEY = 'HB_editor_autoSaveOn';
const BREWKEY = 'HB_newPage_content';
Expand Down Expand Up @@ -216,7 +217,7 @@ const EditPage = (props)=>{
const brewToSave = {
...brew,
text : brew.text.normalize('NFC'),
pageCount : ((brew.renderer === 'legacy' ? brew.text.match(/\\page/g) : brew.text.match(/^\\page$/gm)) || []).length + 1,
pageCount : ((brew.renderer === 'legacy' ? brew.text.match(/\\page/g) : brew.text.match(regexList.v3.pageBreak)) || []).length + 1,
patches : stringifyPatches(makePatches(encodeURI(lastSavedBrew.current.text.normalize('NFC')), encodeURI(brew.text.normalize('NFC')))),
hash : await md5(lastSavedBrew.current.text.normalize('NFC')),
textBin : undefined,
Expand Down
4 changes: 3 additions & 1 deletion client/homebrew/pages/newPage/newPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const SAVEKEYPREFIX = 'HB_editor_defaultSave_';
const useLocalStorage = true;
const neverSaved = true;

import { regexList } from '@shared/helpers.js';

const NewPage = (props)=>{
props = {
brew : DEFAULT_BREW,
Expand Down Expand Up @@ -156,7 +158,7 @@ const NewPage = (props)=>{
const updatedBrew = { ...currentBrew };
splitTextStyleAndMetadata(updatedBrew);

const pageRegex = updatedBrew.renderer === 'legacy' ? /\\page/g : /^\\page$/gm;
const pageRegex = updatedBrew.renderer === 'legacy' ? /\\page/g : regexList.v3.pageBreak;
updatedBrew.pageCount = (updatedBrew.text.match(pageRegex) || []).length + 1;

const res = await request
Expand Down
Loading