Skip to content

Commit d77a086

Browse files
committed
cleaned up logic so it would use less RAM
1 parent 29f109a commit d77a086

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

src/rules/rule-builder.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import {IgnoreType, IgnoreTypes} from '../utils/ignore-types';
66
import {LinterSettings} from 'src/settings-data';
77
import {App} from 'obsidian';
88

9+
// limit the amount of text that can be written to the logs to try to prevent memory issues
10+
const maxFileSizeLength = 10000;
11+
912
export abstract class RuleBuilderBase {
1013
static #ruleMap = new Map<string, Rule>();
1114
static #ruleBuilderMap = new Map<string, RuleBuilderBase>();
@@ -32,7 +35,12 @@ export abstract class RuleBuilderBase {
3235
try {
3336
const newText = rule.apply(text, options);
3437
timingEnd(rule.alias);
35-
logDebug(newText);
38+
39+
if (newText.length > maxFileSizeLength) {
40+
logDebug(newText.slice(0, maxFileSizeLength -1) + '...');
41+
} else {
42+
logDebug(newText);
43+
}
3644

3745
return [newText, true];
3846
} catch (error) {

src/utils/ignore-types.ts

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {getAllCustomIgnoreSectionsInText, getAllTablesInText, getPositions, MDAs
33
import type {Position} from 'unist';
44
import {replaceTextBetweenStartAndEndWithNewValue} from './strings';
55

6-
export type IgnoreResults = {replacedValues: string[], newText: string};
7-
export type IgnoreFunction = ((text: string, placeholder: string) => IgnoreResults);
6+
// export type IgnoreResults = {replacedValues: string[], newText: string};
7+
export type IgnoreFunction = ((text: string, placeholder: string) => [string[], string]);
88
export type IgnoreType = {replaceAction: MDAstTypes | RegExp | IgnoreFunction, placeholder: string};
99

1010
export const IgnoreTypes: Record<string, IgnoreType> = {
@@ -40,19 +40,18 @@ export function ignoreListOfTypes(ignoreTypes: IgnoreType[], text: string, func:
4040
let setOfPlaceholders: {placeholder: string, replacedValues: string[]}[] = [];
4141

4242
// replace ignore blocks with their placeholders
43+
let replaceValues: string[] = [];
4344
for (const ignoreType of ignoreTypes) {
44-
let ignoredResult: IgnoreResults;
4545
if (typeof ignoreType.replaceAction === 'string') { // mdast
46-
ignoredResult = replaceMdastType(text, ignoreType.placeholder, ignoreType.replaceAction);
46+
[replaceValues, text] = replaceMdastType(text, ignoreType.placeholder, ignoreType.replaceAction);
4747
} else if (ignoreType.replaceAction instanceof RegExp) {
48-
ignoredResult = replaceRegex(text, ignoreType.placeholder, ignoreType.replaceAction);
48+
[replaceValues, text] = replaceRegex(text, ignoreType.placeholder, ignoreType.replaceAction);
4949
} else if (typeof ignoreType.replaceAction === 'function') {
5050
const ignoreFunc: IgnoreFunction = ignoreType.replaceAction;
51-
ignoredResult = ignoreFunc(text, ignoreType.placeholder);
51+
[replaceValues, text] = ignoreFunc(text, ignoreType.placeholder);
5252
}
5353

54-
text = ignoredResult.newText;
55-
setOfPlaceholders.push({replacedValues: ignoredResult.replacedValues, placeholder: ignoreType.placeholder});
54+
setOfPlaceholders.push({replacedValues: replaceValues, placeholder: ignoreType.placeholder});
5655
}
5756

5857
text = func(text);
@@ -80,7 +79,7 @@ export function ignoreListOfTypes(ignoreTypes: IgnoreType[], text: string, func:
8079
* @return {string} The text with mdast nodes types specified replaced
8180
* @return {string[]} The mdast nodes values replaced
8281
*/
83-
function replaceMdastType(text: string, placeholder: string, type: MDAstTypes): IgnoreResults {
82+
function replaceMdastType(text: string, placeholder: string, type: MDAstTypes): [string[], string] {
8483
let positions: Position[] = getPositions(type, text);
8584
const replacedValues: string[] = [];
8685

@@ -91,13 +90,16 @@ function replaceMdastType(text: string, placeholder: string, type: MDAstTypes):
9190
for (const position of positions) {
9291
const valueToReplace = text.substring(position.start.offset, position.end.offset);
9392
replacedValues.push(valueToReplace);
93+
}
94+
95+
for (const position of positions) {
9496
text = replaceTextBetweenStartAndEndWithNewValue(text, position.start.offset, position.end.offset, placeholder);
9597
}
9698

9799
// Reverse the replaced values so that they are in the same order as the original text
98100
replacedValues.reverse();
99101

100-
return {newText: text, replacedValues};
102+
return [replacedValues, text];
101103
}
102104

103105
/**
@@ -108,7 +110,7 @@ function replaceMdastType(text: string, placeholder: string, type: MDAstTypes):
108110
* @return {string} The text with regex matches replaced
109111
* @return {string[]} The regex matches replaced
110112
*/
111-
function replaceRegex(text: string, placeholder: string, regex: RegExp): IgnoreResults {
113+
function replaceRegex(text: string, placeholder: string, regex: RegExp): [string[], string] {
112114
const regexMatches = text.match(regex);
113115
const textMatches: string[] = [];
114116
if (regex.flags.includes('g')) {
@@ -127,7 +129,7 @@ function replaceRegex(text: string, placeholder: string, regex: RegExp): IgnoreR
127129
}
128130
}
129131

130-
return {newText: text, replacedValues: textMatches};
132+
return [textMatches, text];
131133
}
132134

133135
/**
@@ -137,10 +139,12 @@ function replaceRegex(text: string, placeholder: string, regex: RegExp): IgnoreR
137139
* @return {string} The text with links replaced
138140
* @return {string[]} The regular markdown links replaced
139141
*/
140-
function replaceMarkdownLinks(text: string, regularLinkPlaceholder: string): IgnoreResults {
142+
function replaceMarkdownLinks(text: string, regularLinkPlaceholder: string): [string[], string] {
141143
const positions: Position[] = getPositions(MDAstTypes.Link, text);
142144
const replacedRegularLinks: string[] = [];
143145

146+
147+
const positionsToReplace: Position [] = [];
144148
for (const position of positions) {
145149
if (position == undefined) {
146150
continue;
@@ -152,54 +156,64 @@ function replaceMarkdownLinks(text: string, regularLinkPlaceholder: string): Ign
152156
continue;
153157
}
154158

159+
positionsToReplace.push(position);
155160
replacedRegularLinks.push(regularLink);
161+
}
162+
163+
for (const position of positionsToReplace) {
156164
text = replaceTextBetweenStartAndEndWithNewValue(text, position.start.offset, position.end.offset, regularLinkPlaceholder);
157165
}
158166

159167
// Reverse the regular links so that they are in the same order as the original text
160168
replacedRegularLinks.reverse();
161169

162-
return {newText: text, replacedValues: replacedRegularLinks};
170+
return [replacedRegularLinks, text];
163171
}
164172

165-
function replaceTags(text: string, placeholder: string): IgnoreResults {
173+
function replaceTags(text: string, placeholder: string): [string[], string] {
166174
const replacedValues: string[] = [];
167175

168176
text = text.replace(tagWithLeadingWhitespaceRegex, (_, whitespace, tag) => {
169177
replacedValues.push(tag);
170178
return whitespace + placeholder;
171179
});
172180

173-
return {newText: text, replacedValues: replacedValues};
181+
return [replacedValues, text];
174182
}
175183

176-
function replaceTables(text: string, tablePlaceholder: string): IgnoreResults {
184+
function replaceTables(text: string, tablePlaceholder: string): [string[], string] {
177185
const tablePositions = getAllTablesInText(text);
178186

179187
const replacedTables: string[] = new Array(tablePositions.length);
180188
let index = 0;
181189
const length = replacedTables.length;
182190
for (const tablePosition of tablePositions) {
183191
replacedTables[length - 1 - index++] = text.substring(tablePosition.startIndex, tablePosition.endIndex);
192+
}
193+
194+
for (const tablePosition of tablePositions) {
184195
text = replaceTextBetweenStartAndEndWithNewValue(text, tablePosition.startIndex, tablePosition.endIndex, tablePlaceholder);
185196
}
186197

187-
return {newText: text, replacedValues: replacedTables};
198+
return [replacedTables, text];
188199
}
189200

190201

191-
function replaceCustomIgnore(text: string, customIgnorePlaceholder: string): IgnoreResults {
202+
function replaceCustomIgnore(text: string, customIgnorePlaceholder: string): [string[], string] {
192203
const customIgnorePositions = getAllCustomIgnoreSectionsInText(text);
193204

194205
const replacedSections: string[] = new Array(customIgnorePositions.length);
195206
let index = 0;
196207
const length = replacedSections.length;
197208
for (const customIgnorePosition of customIgnorePositions) {
198209
replacedSections[length - 1 - index++] = text.substring(customIgnorePosition.startIndex, customIgnorePosition.endIndex);
210+
}
211+
212+
for (const customIgnorePosition of customIgnorePositions) {
199213
text = replaceTextBetweenStartAndEndWithNewValue(text, customIgnorePosition.startIndex, customIgnorePosition.endIndex, customIgnorePlaceholder);
200214
}
201215

202-
return {newText: text, replacedValues: replacedSections};
216+
return [replacedSections, text];
203217
}
204218

205219
function removeOverlappingPositions(positions: Position[]): Position[] {

0 commit comments

Comments
 (0)