Skip to content

Commit 223be97

Browse files
authored
Merge pull request #1310 from pjkaufman/master
Fix: RAM Usage Larger Than It Should Be
2 parents 29f109a + 5818488 commit 223be97

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-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: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ 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 IgnoreFunction = ((text: string, placeholder: string) => [string[], string]);
87
export type IgnoreType = {replaceAction: MDAstTypes | RegExp | IgnoreFunction, placeholder: string};
98

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

4241
// replace ignore blocks with their placeholders
42+
let replaceValues: string[] = [];
4343
for (const ignoreType of ignoreTypes) {
44-
let ignoredResult: IgnoreResults;
4544
if (typeof ignoreType.replaceAction === 'string') { // mdast
46-
ignoredResult = replaceMdastType(text, ignoreType.placeholder, ignoreType.replaceAction);
45+
[replaceValues, text] = replaceMdastType(text, ignoreType.placeholder, ignoreType.replaceAction);
4746
} else if (ignoreType.replaceAction instanceof RegExp) {
48-
ignoredResult = replaceRegex(text, ignoreType.placeholder, ignoreType.replaceAction);
47+
[replaceValues, text] = replaceRegex(text, ignoreType.placeholder, ignoreType.replaceAction);
4948
} else if (typeof ignoreType.replaceAction === 'function') {
5049
const ignoreFunc: IgnoreFunction = ignoreType.replaceAction;
51-
ignoredResult = ignoreFunc(text, ignoreType.placeholder);
50+
[replaceValues, text] = ignoreFunc(text, ignoreType.placeholder);
5251
}
5352

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

5856
text = func(text);
@@ -80,7 +78,7 @@ export function ignoreListOfTypes(ignoreTypes: IgnoreType[], text: string, func:
8078
* @return {string} The text with mdast nodes types specified replaced
8179
* @return {string[]} The mdast nodes values replaced
8280
*/
83-
function replaceMdastType(text: string, placeholder: string, type: MDAstTypes): IgnoreResults {
81+
function replaceMdastType(text: string, placeholder: string, type: MDAstTypes): [string[], string] {
8482
let positions: Position[] = getPositions(type, text);
8583
const replacedValues: string[] = [];
8684

@@ -91,13 +89,16 @@ function replaceMdastType(text: string, placeholder: string, type: MDAstTypes):
9189
for (const position of positions) {
9290
const valueToReplace = text.substring(position.start.offset, position.end.offset);
9391
replacedValues.push(valueToReplace);
92+
}
93+
94+
for (const position of positions) {
9495
text = replaceTextBetweenStartAndEndWithNewValue(text, position.start.offset, position.end.offset, placeholder);
9596
}
9697

9798
// Reverse the replaced values so that they are in the same order as the original text
9899
replacedValues.reverse();
99100

100-
return {newText: text, replacedValues};
101+
return [replacedValues, text];
101102
}
102103

103104
/**
@@ -108,7 +109,7 @@ function replaceMdastType(text: string, placeholder: string, type: MDAstTypes):
108109
* @return {string} The text with regex matches replaced
109110
* @return {string[]} The regex matches replaced
110111
*/
111-
function replaceRegex(text: string, placeholder: string, regex: RegExp): IgnoreResults {
112+
function replaceRegex(text: string, placeholder: string, regex: RegExp): [string[], string] {
112113
const regexMatches = text.match(regex);
113114
const textMatches: string[] = [];
114115
if (regex.flags.includes('g')) {
@@ -127,7 +128,7 @@ function replaceRegex(text: string, placeholder: string, regex: RegExp): IgnoreR
127128
}
128129
}
129130

130-
return {newText: text, replacedValues: textMatches};
131+
return [textMatches, text];
131132
}
132133

133134
/**
@@ -137,10 +138,12 @@ function replaceRegex(text: string, placeholder: string, regex: RegExp): IgnoreR
137138
* @return {string} The text with links replaced
138139
* @return {string[]} The regular markdown links replaced
139140
*/
140-
function replaceMarkdownLinks(text: string, regularLinkPlaceholder: string): IgnoreResults {
141+
function replaceMarkdownLinks(text: string, regularLinkPlaceholder: string): [string[], string] {
141142
const positions: Position[] = getPositions(MDAstTypes.Link, text);
142143
const replacedRegularLinks: string[] = [];
143144

145+
146+
const positionsToReplace: Position [] = [];
144147
for (const position of positions) {
145148
if (position == undefined) {
146149
continue;
@@ -152,54 +155,64 @@ function replaceMarkdownLinks(text: string, regularLinkPlaceholder: string): Ign
152155
continue;
153156
}
154157

158+
positionsToReplace.push(position);
155159
replacedRegularLinks.push(regularLink);
160+
}
161+
162+
for (const position of positionsToReplace) {
156163
text = replaceTextBetweenStartAndEndWithNewValue(text, position.start.offset, position.end.offset, regularLinkPlaceholder);
157164
}
158165

159166
// Reverse the regular links so that they are in the same order as the original text
160167
replacedRegularLinks.reverse();
161168

162-
return {newText: text, replacedValues: replacedRegularLinks};
169+
return [replacedRegularLinks, text];
163170
}
164171

165-
function replaceTags(text: string, placeholder: string): IgnoreResults {
172+
function replaceTags(text: string, placeholder: string): [string[], string] {
166173
const replacedValues: string[] = [];
167174

168175
text = text.replace(tagWithLeadingWhitespaceRegex, (_, whitespace, tag) => {
169176
replacedValues.push(tag);
170177
return whitespace + placeholder;
171178
});
172179

173-
return {newText: text, replacedValues: replacedValues};
180+
return [replacedValues, text];
174181
}
175182

176-
function replaceTables(text: string, tablePlaceholder: string): IgnoreResults {
183+
function replaceTables(text: string, tablePlaceholder: string): [string[], string] {
177184
const tablePositions = getAllTablesInText(text);
178185

179186
const replacedTables: string[] = new Array(tablePositions.length);
180187
let index = 0;
181188
const length = replacedTables.length;
182189
for (const tablePosition of tablePositions) {
183190
replacedTables[length - 1 - index++] = text.substring(tablePosition.startIndex, tablePosition.endIndex);
191+
}
192+
193+
for (const tablePosition of tablePositions) {
184194
text = replaceTextBetweenStartAndEndWithNewValue(text, tablePosition.startIndex, tablePosition.endIndex, tablePlaceholder);
185195
}
186196

187-
return {newText: text, replacedValues: replacedTables};
197+
return [replacedTables, text];
188198
}
189199

190200

191-
function replaceCustomIgnore(text: string, customIgnorePlaceholder: string): IgnoreResults {
201+
function replaceCustomIgnore(text: string, customIgnorePlaceholder: string): [string[], string] {
192202
const customIgnorePositions = getAllCustomIgnoreSectionsInText(text);
193203

194204
const replacedSections: string[] = new Array(customIgnorePositions.length);
195205
let index = 0;
196206
const length = replacedSections.length;
197207
for (const customIgnorePosition of customIgnorePositions) {
198208
replacedSections[length - 1 - index++] = text.substring(customIgnorePosition.startIndex, customIgnorePosition.endIndex);
209+
}
210+
211+
for (const customIgnorePosition of customIgnorePositions) {
199212
text = replaceTextBetweenStartAndEndWithNewValue(text, customIgnorePosition.startIndex, customIgnorePosition.endIndex, customIgnorePlaceholder);
200213
}
201214

202-
return {newText: text, replacedValues: replacedSections};
215+
return [replacedSections, text];
203216
}
204217

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

0 commit comments

Comments
 (0)