Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Compensate for code and fenced blocks

AuthorDaveJarvis <email>
Date2020-07-01 10:43:24 GMT-0700
Commitb4fd2969db605e828d7651f1955d365e51b56935
Parent6b01b2e
Delta25 lines added, 4 lines removed, 21-line increase
src/main/java/com/scrivenvar/editors/markdown/MarkdownEditorPane.java
public int approximateParagraphId( final int paraIndex ) {
final StyleClassedTextArea editor = getEditor();
+ final List<String> lines = new ArrayList<>( 4096 );
+
int i = 0;
String prevText = "";
-
- final List<String> lines = new ArrayList<>( 4096 );
+ boolean withinFencedBlock = false;
+ boolean withinCodeBlock = false;
for( final var p : editor.getParagraphs() ) {
if( i > paraIndex ) {
break;
}
final String text = p.getText().replace( '>', ' ' );
- if( (!text.isBlank() && prevText.isBlank()) ||
- PATTERN_NEW_LINE.matcher( text ).matches() ) {
+ if( text.startsWith( "```" ) ) {
+ if( withinFencedBlock = !withinFencedBlock ) {
+ lines.add( text );
+ }
+ }
+
+ if( !withinFencedBlock ) {
+ final boolean foundCodeBlock = text.startsWith( " " );
+
+ if( foundCodeBlock && !withinCodeBlock ) {
+ lines.add( text );
+ withinCodeBlock = true;
+ }
+ else if( !foundCodeBlock ) {
+ withinCodeBlock = false;
+ }
+ }
+
+ if( !withinFencedBlock && !withinCodeBlock &&
+ ((!text.isBlank() && prevText.isBlank()) ||
+ PATTERN_NEW_LINE.matcher( text ).matches()) ) {
lines.add( text );
}