WE have a markdown validator that authors use to validate their md files, One check in that is the liquid tag validation where we check for invalid liquid tags in the md files. My requirement is to add a validation to check whether the flower braces are used properly and are properly terminated. eg if an author opens 5 { , there should be equally 5 closing }. But we also have to exclude code blocks from this validation. any brackets inside a code block should not be validated. code blocks may be a 4 space indentation, 3 backticks or a single backtick.
bracecountopen=$(grep -o -i '{' $file | wc -l); bracecountclose=$(grep -o -i '}' $file | wc -l); if [ "$bracecountopen" != "$bracecountclose" ]; then throw error fi for excluding code blocks
var+=$(grep '^\ \ \ \ [^>]' $file); while IFS= read -n1 char; do if [[ "$char" == "\`" ]]; then if [ $flag = 1 ]; then flag=0; continue fi flag=1; fi if [ $flag = 1 ]; then # var+=$char; echo "" fi done <$file codebraceopen=$(echo "$var" | grep -o -i '{' | wc -l) codebraceclose=$(echo "$var" | grep -o -i '}' | wc -l) let "bracecountopen=bracecountopen - codebraceopen" let "bracecountclose=bracecountclose - codebraceclose" Is this approach correct? Is there any better logic to achieve this? The problem here is some author add a tab or 4 spaces before starting their code block with back ticks which makes the content appear twice in the 'var' , it is counted in the 4 space as well as the backticks check. How to fix this?
}}} {{{. It would be best if you used some external Markdown validation tool to do this job. Unfortunately, I'm unaware of what's available for doing this. Also note that double and triple backtick is used for different types of code, not just single backticks.{}is legit,}{is not) when counting opening and closing braces, OP's first code block needs to keep track of the order in which braces appear in parsed file. If you check for syntactic correctness then the simplest test that come to mind as you parse your file is that the nbr of opening braces should be >= to the nbr of closing braces at all time during parsing and counting, excluding code blocks as noted.{{new line}new line}? This is what my answer resolve!