mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-14 19:22:52 +00:00
Detect heading from Markdown Lexer tokens
This commit is contained in:
@@ -2,18 +2,17 @@ module.exports = {
|
|||||||
createFooterFunc : function(headerSize=1){
|
createFooterFunc : function(headerSize=1){
|
||||||
return (props)=>{
|
return (props)=>{
|
||||||
const cursorPos = props.cursorPos;
|
const cursorPos = props.cursorPos;
|
||||||
|
const renderer = props.brew.renderer || 'V3';
|
||||||
|
|
||||||
let header='';
|
const Markdown = renderer == 'V3' ? require('../../../../shared/naturalcrit/markdown.js') : require('../../../../shared/naturalcrit/markdownLegacy.js');
|
||||||
while (header.length < headerSize) {
|
|
||||||
header = header.concat('#');
|
|
||||||
}
|
|
||||||
header = header.concat(' ');
|
|
||||||
|
|
||||||
const textArray = props.brew.text.split('\n').slice(0, cursorPos.line).reverse();
|
const markdownText = props.brew.text.split('\n').slice(0, cursorPos.line).join('\n');
|
||||||
const textArrayFilter = textArray.filter((line)=>{ return line.slice(0, header.length) == header; });
|
const markdownTokens = Markdown.marked.lexer(markdownText);
|
||||||
const text = textArrayFilter[0]?.slice(header.length).trim() || 'PART 1 | SECTION NAME';
|
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`;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user