0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-28 22:12:39 +00:00

Detect heading from Markdown Lexer tokens

This commit is contained in:
G.Ambatte
2023-06-23 17:58:54 +12:00
parent e7224e97ef
commit 949d763e35

View File

@@ -2,18 +2,17 @@ module.exports = {
createFooterFunc : function(headerSize=1){
return (props)=>{
const cursorPos = props.cursorPos;
const renderer = props.brew.renderer || 'V3';
let header='';
while (header.length < headerSize) {
header = header.concat('#');
}
header = header.concat(' ');
const Markdown = renderer == 'V3' ? require('../../../../shared/naturalcrit/markdown.js') : require('../../../../shared/naturalcrit/markdownLegacy.js');
const textArray = props.brew.text.split('\n').slice(0, cursorPos.line).reverse();
const textArrayFilter = textArray.filter((line)=>{ return line.slice(0, header.length) == header; });
const text = textArrayFilter[0]?.slice(header.length).trim() || 'PART 1 | SECTION NAME';
const markdownText = props.brew.text.split('\n').slice(0, cursorPos.line).join('\n');
const markdownTokens = Markdown.marked.lexer(markdownText);
const headerToken = markdownTokens.findLast((lexerToken)=>{ return lexerToken.type === 'heading' && lexerToken.depth === headerSize; });
const headerText = headerToken?.tokens.map((token)=>{ return token.text; }).join('');
const outputText = headerText || 'PART 1 | SECTION NAME';
return `\n{{footnote ${text}}}\n`;
return `\n{{footnote ${outputText}}}\n`;
};
}
};