Skip to content

Commit 2231002

Browse files
committed
feat(queries): add multi-line queries support
Features some refactoring to support this feature, to remove the dependency of compiling a complex reg-exp I preferred keeping the output of the findQueries function untransformed, so we can use that string to find and replace the query with the serialized query. It adds a boolean to check if we are capturing a multi-line query or not, and some logic to complete the feature. I've tested both multi-line and single-line queries to ensure that I did not break basic functionality.
1 parent f700ff0 commit 2231002

File tree

6 files changed

+42
-37
lines changed

6 files changed

+42
-37
lines changed

apps/plugin/src/app/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export const NOTICE_TIMEOUT = 5000;
66
export const DEFAULT_CANVAS_FILE_NAME = 'Canvas.md';
77
export const MARKDOWN_FILE_EXTENSION = 'md';
88

9-
export const QUERY_FLAG_OPEN = `<!-- QueryToSerialize: `;
10-
export const QUERY_FLAG_CLOSE = ` -->`;
9+
export const QUERY_FLAG_OPEN = `<!-- QueryToSerialize:`;
10+
export const QUERY_FLAG_CLOSE = `-->`;
1111

1212
// Query and serialized query structure: <!-- SerializedQuery: QUERY -->\n<markdown>\n<!-- SerializedQuery END -->
1313
export const SERIALIZED_QUERY_START = `<!-- SerializedQuery: `;

apps/plugin/src/app/plugin.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { DataviewApi } from 'obsidian-dataview/lib/api/plugin-api';
2121
import { add, isBefore } from 'date-fns';
2222
import { serializeQuery } from './utils/serialize-query.fn';
2323
import { findQueries } from './utils/find-queries.fn';
24-
import { escapeRegExp } from './utils/escape-reg-exp.fn';
2524
import { isTableQuery } from './utils/is-table-query.fn';
2625

2726
export class DataviewSerializerPlugin extends Plugin {
@@ -227,7 +226,7 @@ export class DataviewSerializerPlugin extends Plugin {
227226
//log(`Processing query: [${foundQuery}] in file [${file.path}]`, 'debug');
228227
// Reference: https://github.com/IdreesInc/Waypoint/blob/master/main.ts
229228
const serializedQuery = await serializeQuery({
230-
query: foundQuery,
229+
query: foundQuery.trim(),
231230
originFile: file.path,
232231
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
233232
dataviewApi: this.dataviewApi!,
@@ -236,24 +235,18 @@ export class DataviewSerializerPlugin extends Plugin {
236235
//log('Serialized query: ', 'debug', serializedQuery);
237236

238237
if ('' !== serializedQuery) {
239-
const escapedQuery = escapeRegExp(foundQuery);
240-
241-
const queryToSerializeRegex = new RegExp(
242-
`${QUERY_FLAG_OPEN}${escapedQuery}.*${QUERY_FLAG_CLOSE}\\n`,
243-
'gm'
244-
);
245-
246238
let queryAndSerializedQuery = '';
247239
if (isTableQuery(foundQuery)) {
248-
queryAndSerializedQuery = `${QUERY_FLAG_OPEN}${foundQuery}${QUERY_FLAG_CLOSE}\n${SERIALIZED_QUERY_START}${foundQuery}${QUERY_FLAG_CLOSE}\n\n${serializedQuery}${SERIALIZED_QUERY_END}\n`;
240+
queryAndSerializedQuery = `${QUERY_FLAG_OPEN}${foundQuery}${QUERY_FLAG_CLOSE}\n${SERIALIZED_QUERY_START}${QUERY_FLAG_CLOSE}\n\n${serializedQuery}${SERIALIZED_QUERY_END}\n`;
249241
} else {
250-
queryAndSerializedQuery = `${QUERY_FLAG_OPEN}${foundQuery}${QUERY_FLAG_CLOSE}\n${SERIALIZED_QUERY_START}${foundQuery}${QUERY_FLAG_CLOSE}\n${serializedQuery}${SERIALIZED_QUERY_END}\n`;
242+
queryAndSerializedQuery = `${QUERY_FLAG_OPEN}${foundQuery}${QUERY_FLAG_CLOSE}\n${SERIALIZED_QUERY_START}${QUERY_FLAG_CLOSE}\n${serializedQuery}${SERIALIZED_QUERY_END}\n`;
251243
}
252244
//log('Query to serialize regex: ', 'debug', queryToSerializeRegex);
253245

254246
//log('Updated text before: ', 'debug', updatedText);
247+
const searchedString = `${QUERY_FLAG_OPEN}${foundQuery}${QUERY_FLAG_CLOSE}`;
255248
updatedText = updatedText.replace(
256-
queryToSerializeRegex,
249+
searchedString,
257250
queryAndSerializedQuery
258251
);
259252
//log('Updated text after: ', 'debug', updatedText);

apps/plugin/src/app/utils/escape-reg-exp.fn.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

apps/plugin/src/app/utils/find-queries.fn.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,45 @@ import { isSupportedQueryType } from './is-supported-query-type.fn';
77
*/
88
export const findQueries = (text: string): string[] => {
99
const retVal: string[] = [];
10+
let isCapturing = false;
11+
let foundQuery = '';
1012

1113
const lines: string[] = text.split('\n');
1214
for (const line of lines) {
13-
const trimmedLine = line.trim();
14-
if (
15-
trimmedLine.startsWith(QUERY_FLAG_OPEN) &&
16-
trimmedLine.endsWith(QUERY_FLAG_CLOSE)
17-
) {
18-
let foundQuery = trimmedLine.replace(QUERY_FLAG_OPEN, '');
19-
foundQuery = foundQuery.replace(QUERY_FLAG_CLOSE, '');
20-
foundQuery = foundQuery.trim();
15+
if (isCapturing) {
16+
if (line.includes(QUERY_FLAG_CLOSE)) {
17+
const endIndex = line.indexOf(QUERY_FLAG_CLOSE);
18+
foundQuery += `${line.substring(0, endIndex)}`;
2119

22-
// Ignore duplicates
23-
// Make sure it is a supported query
24-
if (!retVal.includes(foundQuery) && isSupportedQueryType(foundQuery)) {
25-
retVal.push(foundQuery);
20+
if (!retVal.includes(foundQuery) && isSupportedQueryType(foundQuery)) {
21+
retVal.push(foundQuery);
22+
isCapturing = false;
23+
foundQuery = '';
24+
}
25+
} else {
26+
// Accumulate the current line if capturing multi-line query
27+
foundQuery += `${line}\n`;
2628
}
2729
}
28-
}
30+
// Detect QUERY FLAG OPEN and single line comments
31+
if (!isCapturing && line.includes(QUERY_FLAG_OPEN)) {
32+
isCapturing = true;
33+
const startIndex = line.indexOf(QUERY_FLAG_OPEN) + QUERY_FLAG_OPEN.length;
34+
foundQuery = line.substring(startIndex) + '\n';
35+
if (line.includes(QUERY_FLAG_CLOSE)) {
36+
const endIndex = line.indexOf(QUERY_FLAG_CLOSE);
37+
foundQuery = line.substring(startIndex, endIndex);
2938

39+
// Ignore duplicates
40+
// Make sure it is a supported query
41+
if (!retVal.includes(foundQuery) && isSupportedQueryType(foundQuery)) {
42+
retVal.push(foundQuery);
43+
isCapturing = false;
44+
foundQuery = '';
45+
}
46+
}
47+
}
48+
}
49+
console.log(retVal);
3050
return retVal;
3151
};

apps/plugin/src/app/utils/is-supported-query-type.fn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { SUPPORTED_QUERY_TYPES } from '../constants';
77
export const isSupportedQueryType = (query: string): boolean => {
88
let retVal = false;
99

10-
const queryLower = query.toLowerCase();
10+
const queryLower = query.trim().toLowerCase();
1111

1212
for (const queryType of SUPPORTED_QUERY_TYPES) {
1313
if (queryLower.startsWith(queryType)) {

apps/plugin/src/app/utils/is-table-query.fn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { QUERY_TYPE_TABLE } from '../constants';
77
export const isTableQuery = (query: string): boolean => {
88
let retVal = false;
99

10-
const queryLower = query.toLowerCase();
10+
const queryLower = query.trim().toLowerCase();
1111

1212
if (queryLower.startsWith(QUERY_TYPE_TABLE)) {
1313
retVal = true;

0 commit comments

Comments
 (0)