Skip to content

Commit

Permalink
getBlock supports sections
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardoferretti committed May 19, 2023
1 parent 09e13f7 commit f297139
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 10 deletions.
76 changes: 75 additions & 1 deletion packages/foam-vscode/src/core/services/markdown-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Logger } from '../utils/log';
import { URI } from '../model/uri';
import { Range } from '../model/range';
import { getRandomURI } from '../../test/test-utils';
import { Position } from '../model/position';

Logger.setLevel('error');

Expand Down Expand Up @@ -464,7 +465,7 @@ But with some content.
});
});

describe('Block detection', () => {
describe('Block detection for lists', () => {
const md = `
- this is block 1
- this is [[block]] 2
Expand Down Expand Up @@ -508,3 +509,76 @@ this is another simple line
- this is block 2.1`);
});
});

describe('block detection for sections', () => {
const markdown = `
# Section 1
- this is block 1
- this is [[block]] 2
- this is block 2.1
# Section 2
this is a simple line
this is another simple line
## Section 2.1
- this is block 3.1
- this is block 3.1.1
- this is block 3.2
# Section 3
# Section 4
some text
some text
`;

it('should return correct block for valid markdown string with line number', () => {
const { block, nLines } = getBlockFor(markdown, 1);
expect(block).toEqual(`# Section 1
- this is block 1
- this is [[block]] 2
- this is block 2.1
`);
expect(nLines).toEqual(5);
});

it('should return correct block for valid markdown string with position', () => {
const { block, nLines } = getBlockFor(markdown, 6);
expect(block).toEqual(`# Section 2
this is a simple line
this is another simple line
## Section 2.1
- this is block 3.1
- this is block 3.1.1
- this is block 3.2
`);
expect(nLines).toEqual(9);
});

it('should return single line for section with no content', () => {
const { block, nLines } = getBlockFor(markdown, 15);
expect(block).toEqual('# Section 3');
expect(nLines).toEqual(1);
});

it('should return till end of file for last section', () => {
const { block, nLines } = getBlockFor(markdown, 16);
expect(block).toEqual(`# Section 4
some text
some text`);
expect(nLines).toEqual(3);
});

it('should return single line for non-existing line number', () => {
const { block, nLines } = getBlockFor(markdown, 100);
expect(block).toEqual('');
expect(nLines).toEqual(1);
});

it('should return single line for non-existing position', () => {
const { block, nLines } = getBlockFor(markdown, Position.create(100, 2));
expect(block).toEqual('');
expect(nLines).toEqual(1);
});
});
36 changes: 27 additions & 9 deletions packages/foam-vscode/src/core/services/markdown-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,19 +433,37 @@ export const getBlockFor = (
const searchLine = typeof line === 'number' ? line : line.line;
const tree = blockParser.parse(markdown);
const lines = markdown.split('\n');
let block = null;
let nLines = 0;
let startLine = -1;
let endLine = -1;

// For list items, we also include the sub-lists
visit(tree, ['listItem'], (node: any) => {
if (node.position.start.line === searchLine + 1) {
block = lines
.slice(node.position.start.line - 1, node.position.end.line)
.join('\n');
nLines = node.position.end.line - node.position.start.line;
startLine = node.position.start.line - 1;
endLine = node.position.end.line;
return visit.EXIT;
}
});
if (block == null) {
block = lines[searchLine];
}

// For headings, we also include the sub-sections
let headingLevel = -1;
visit(tree, ['heading'], (node: any) => {
if (startLine > -1 && node.depth <= headingLevel) {
endLine = node.position.start.line - 1;
return visit.EXIT;
}
if (node.position.start.line === searchLine + 1) {
headingLevel = node.depth;
startLine = node.position.start.line - 1;
endLine = lines.length - 1; // in case it's the last section
}
});

let nLines = startLine == -1 ? 1 : endLine - startLine;
let block =
startLine == -1
? lines[searchLine] ?? ''
: lines.slice(startLine, endLine).join('\n');

return { block, nLines };
};

0 comments on commit f297139

Please sign in to comment.