Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternate \page{curlies} #4004

Merged
merged 14 commits into from
Jan 30, 2025
24 changes: 18 additions & 6 deletions client/homebrew/brewRenderer/brewRenderer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const BrewPage = (props)=>{
...props
};
const pageRef = useRef(null);
const cleanText = safeHTML(props.contents);
let cleanText = safeHTML(props.contents);
calculuschild marked this conversation as resolved.
Show resolved Hide resolved

useEffect(()=>{
if(!pageRef.current) return;
Expand Down Expand Up @@ -77,8 +77,8 @@ const BrewPage = (props)=>{
centerObserver.disconnect();
};
}, []);

return <div className={props.className} id={`p${props.index + 1}`} data-index={props.index} ref={pageRef} style={props.style}>
return <div className={props.className} id={`p${props.index + 1}`} data-index={props.index} ref={pageRef} style={props.style} {...props.attributes}>
<div className='columnWrapper' dangerouslySetInnerHTML={{ __html: cleanText }} />
</div>;
};
Expand Down Expand Up @@ -126,7 +126,7 @@ const BrewRenderer = (props)=>{
if(props.renderer == 'legacy') {
rawPages = props.text.split('\\page');
} else {
rawPages = props.text.split(/^\\page$/gm);
rawPages = props.text.split(/^(?=\\page(?:{[^\n{}]+})?$)/gm);
}

const handlePageVisibilityChange = (pageNum, isVisible, isCenter)=>{
Expand Down Expand Up @@ -173,20 +173,32 @@ const BrewRenderer = (props)=>{

const renderPage = (pageText, index)=>{

const styles = {
let styles = {
...(!displayOptions.pageShadows ? { boxShadow: 'none' } : {})
// Add more conditions as needed
};
let attributes = {};

if(props.renderer == 'legacy') {
const html = MarkdownLegacy.render(pageText);

return <BrewPage className='page phb' index={index} key={index} contents={html} style={styles} onVisibilityChange={handlePageVisibilityChange} />;
} else {
if(pageText.startsWith('\\page')) {
let firstLineTokens = Markdown.marked.lexer(pageText.split('\n', 1)[0])[0].tokens;
let injectedTags = firstLineTokens.find(obj => obj.injectedTags !== undefined)?.injectedTags;
if(injectedTags) {
styles = {...styles, ...injectedTags.styles};
styles = _.mapKeys(styles, (v, k) => _.camelCase(k)); // Convert CSS to camelCase for React
attributes = injectedTags.attributes;
}
pageText = pageText.includes('\n') ? pageText.substring(pageText.indexOf('\n') + 1) : ''; // Remove the \page line
}

pageText += `\n\n&nbsp;\n\\column\n&nbsp;`; //Artificial column break at page end to emulate column-fill:auto (until `wide` is used, when column-fill:balance will reappear)
const html = Markdown.render(pageText, index);

return <BrewPage className='page' index={index} key={index} contents={html} style={styles} onVisibilityChange={handlePageVisibilityChange} />;
return <BrewPage className='page' index={index} key={index} contents={html} style={styles} attributes={attributes} onVisibilityChange={handlePageVisibilityChange} />;
}
};

Expand Down
19 changes: 10 additions & 9 deletions client/homebrew/editor/editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ const Editor = createClass({
},

updateCurrentCursorPage : function(cursor) {
const lines = this.props.brew.text.split('\n').slice(0, cursor.line + 1);
const pageRegex = this.props.brew.renderer == 'V3' ? /^\\page$/ : /\\page/;
const lines = this.props.brew.text.split('\n').slice(1, cursor.line + 1);
const pageRegex = this.props.brew.renderer == 'V3' ? /^(?=\\page(?:{[^\n{}]+})?$)/ : /\\page/;
const currentPage = lines.reduce((count, line)=>count + (pageRegex.test(line) ? 1 : 0), 1);
this.props.onCursorPageChange(currentPage);
},

updateCurrentViewPage : function(topScrollLine) {
const lines = this.props.brew.text.split('\n').slice(0, topScrollLine + 1);
const pageRegex = this.props.brew.renderer == 'V3' ? /^\\page$/ : /\\page/;
const lines = this.props.brew.text.split('\n').slice(1, topScrollLine + 1);
const pageRegex = this.props.brew.renderer == 'V3' ? /^(?=\\page(?:{[^\n{}]+})?$)/ : /\\page/;
const currentPage = lines.reduce((count, line)=>count + (pageRegex.test(line) ? 1 : 0), 1);
this.props.onViewPageChange(currentPage);
},
Expand Down Expand Up @@ -174,7 +174,7 @@ const Editor = createClass({

for (let i=customHighlights.length - 1;i>=0;i--) customHighlights[i].clear();

let editorPageCount = 2; // start page count from page 2
let editorPageCount = 1; // start page count from page 1

_.forEach(this.props.brew.text.split('\n'), (line, lineNumber)=>{

Expand All @@ -190,7 +190,10 @@ const Editor = createClass({

// Styling for \page breaks
if((this.props.renderer == 'legacy' && line.includes('\\page')) ||
(this.props.renderer == 'V3' && line.match(/^\\page$/))) {
(this.props.renderer == 'V3' && line.match(/^(?=\\page(?:{[^\n{}]+})?$)/))) {

if(lineNumber > 0) // Since \page is optional on first line of document,
editorPageCount += 1; // don't use it to increment page count; stay at 1

// add back the original class 'background' but also add the new class '.pageline'
codeMirror.addLineClass(lineNumber, 'background', 'pageLine');
Expand All @@ -199,8 +202,6 @@ const Editor = createClass({
textContent : editorPageCount
});
codeMirror.setBookmark({ line: lineNumber, ch: line.length }, pageCountElement);

editorPageCount += 1;
};

// New Codemirror styling for V3 renderer
Expand Down Expand Up @@ -358,7 +359,7 @@ const Editor = createClass({
if(!this.isText() || isJumping)
return;

const textSplit = this.props.renderer == 'V3' ? /^\\page$/gm : /\\page/;
const textSplit = this.props.renderer == 'V3' ? /^(?=\\page(?:{[^\n{}]+})?$)/gm : /\\page/;
const textString = this.props.brew.text.split(textSplit).slice(0, targetPage-1).join(textSplit);
const targetLine = textString.match('\n') ? textString.split('\n').length - 1 : -1;

Expand Down
2 changes: 1 addition & 1 deletion shared/naturalcrit/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ const Markdown = {
});

return errors;
},
}
};

export default Markdown;
Expand Down