Skip to content

Commit 3e809e7

Browse files
committed
Mostly cleanup
1 parent 0542bc4 commit 3e809e7

10 files changed

Lines changed: 166 additions & 220 deletions

‎main.js‎

Lines changed: 42 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package-lock.json‎

Lines changed: 0 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
"devDependencies": {
1919
"@eslint/js": "^9.39.5",
2020
"@stylistic/stylelint-plugin": "^2.1.2",
21-
"@types/dompurify": "^3.0.3",
2221
"@types/node": "^16.11.6",
23-
"@types/validator": "^13.12.0",
2422
"concurrently": "^9.2.1",
2523
"esbuild": "^0.28.2",
2624
"eslint": "^10.14.0",

‎src/CodeBlockLinks.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export function getDisplayNameAndReference(input: string): { displayName: string, reference: string } {
2+
const displayNameMarker = "|";
3+
const referenceMarker = "#";
4+
5+
const displayNameIndex = input.lastIndexOf(displayNameMarker);
6+
const referenceIndex = input.indexOf(referenceMarker);
7+
8+
const result: { displayName: string, reference: string } = {
9+
displayName: '',
10+
reference: ''
11+
};
12+
13+
if (displayNameIndex !== -1) {
14+
result.displayName = input.substring(displayNameIndex);
15+
}
16+
17+
if (referenceIndex !== -1) {
18+
result.reference = input.substring(referenceIndex, displayNameIndex !== -1 ? displayNameIndex : undefined);
19+
}
20+
21+
return result;
22+
}// getDisplayNameAndReference
23+
24+
// the new text of the link [[linkText]] after "oldPath" was renamed to "newPath", or null if the link stays as it is
25+
// "linkResolves" - the link still points to an existing file ==> it isn't a link to the renamed one
26+
export function getRenamedLinkText(linkText: string, oldPath: string, newPath: string, newExtension: string | null, linkResolves: boolean): string | null {
27+
if (linkResolves) {
28+
return null;
29+
}
30+
31+
const { displayName, reference } = getDisplayNameAndReference(linkText);
32+
const target = linkText.split('|')[0].split('#')[0];
33+
const hasFolder = target.includes('/');
34+
const oldPathWithoutFolder = oldPath.split('/').slice(-1)[0];
35+
36+
let isLinkToOldPath: boolean;
37+
let newTarget: string;
38+
if (newExtension !== null && target.includes(newExtension)) {
39+
// SomeFolder/Untitled 22.md or Untitled 22.md
40+
isLinkToOldPath = target.toLowerCase() === (hasFolder ? oldPath : oldPathWithoutFolder).toLowerCase();
41+
newTarget = newPath;
42+
} else {
43+
// SomeFolder/Untitled 22 or Untitled 22
44+
const oldTarget = (hasFolder ? oldPath : oldPathWithoutFolder).replace(/\.[^.]*$/, '');
45+
isLinkToOldPath = oldTarget.length > 0 && target.toLowerCase() === oldTarget.toLowerCase();
46+
newTarget = newPath.replace(/\.[^.]*$/, '');
47+
}
48+
49+
return isLinkToOldPath ? newTarget + reference + displayName : null;
50+
}// getRenamedLinkText

‎src/StateStore.ts‎

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,30 @@ import { ChangeSet } from "@codemirror/state";
33
type PermanentData<V> = Record<string, Record<string, V>>;
44

55
export class StateStore<K extends string | number, V> {
6-
private _isPermanent: boolean;
6+
isPermanent: boolean;
7+
permanentData: PermanentData<V> = {};
78
private keyParser: (raw: string) => K;
89

910
private session: Map<string, Map<K, V>> = new Map();
10-
private _permanentData: PermanentData<V> = {};
1111

1212
constructor(isPermanent: boolean, keyParser: (raw: string) => K) {
13-
this._isPermanent = isPermanent;
13+
this.isPermanent = isPermanent;
1414
this.keyParser = keyParser;
1515
}
1616

17-
get isPermanent(): boolean {
18-
return this._isPermanent;
19-
}
20-
21-
set isPermanent(value: boolean) {
22-
this._isPermanent = value;
23-
}
24-
25-
get permanentData(): PermanentData<V> {
26-
return this._permanentData;
27-
}
28-
29-
set permanentData(data: PermanentData<V>) {
30-
this._permanentData = data;
31-
}
32-
3317
// get single value
3418
get(filePath: string, key: K): V | undefined {
35-
if (this._isPermanent) {
36-
const record = this._permanentData[filePath];
19+
if (this.isPermanent) {
20+
const record = this.permanentData[filePath];
3721
return record ? record[String(key)] : undefined;
3822
}
3923
return this.session.get(filePath)?.get(key);
4024
}// get
4125

4226
// return all entries for a file as a Map. For permanent data, the record is converted to a Map
4327
getAll(filePath: string): Map<K, V> | undefined {
44-
if (this._isPermanent) {
45-
const record = this._permanentData[filePath];
28+
if (this.isPermanent) {
29+
const record = this.permanentData[filePath];
4630
if (!record) {
4731
return undefined;
4832
}
@@ -53,11 +37,11 @@ export class StateStore<K extends string | number, V> {
5337

5438
// set single value
5539
set(filePath: string, key: K, value: V): void {
56-
if (this._isPermanent) {
57-
if (!this._permanentData[filePath]) {
58-
this._permanentData[filePath] = {};
40+
if (this.isPermanent) {
41+
if (!this.permanentData[filePath]) {
42+
this.permanentData[filePath] = {};
5943
}
60-
this._permanentData[filePath][String(key)] = value;
44+
this.permanentData[filePath][String(key)] = value;
6145
} else {
6246
let map = this.session.get(filePath);
6347
if (!map) {
@@ -70,12 +54,12 @@ export class StateStore<K extends string | number, V> {
7054

7155
// delete a single entry. cleans up empty file records as well
7256
delete(filePath: string, key: K): void {
73-
if (this._isPermanent) {
74-
const record = this._permanentData[filePath];
57+
if (this.isPermanent) {
58+
const record = this.permanentData[filePath];
7559
if (record) {
7660
delete record[String(key)];
7761
if (Object.keys(record).length === 0) {
78-
delete this._permanentData[filePath];
62+
delete this.permanentData[filePath];
7963
}
8064
}
8165
} else {
@@ -92,13 +76,13 @@ export class StateStore<K extends string | number, V> {
9276
// wipe both session and permanent stores
9377
clear(): void {
9478
this.session.clear();
95-
this._permanentData = {};
79+
this.permanentData = {};
9680
}// clear
9781

9882
remap(filePath: string, changes: ChangeSet): boolean {
9983
let changed = false;
100-
if (this._isPermanent) {
101-
const record = this._permanentData[filePath];
84+
if (this.isPermanent) {
85+
const record = this.permanentData[filePath];
10286
if (!record) {
10387
return false;
10488
}
@@ -121,7 +105,7 @@ export class StateStore<K extends string | number, V> {
121105
}
122106
}
123107

124-
this._permanentData[filePath] = newRecord;
108+
this.permanentData[filePath] = newRecord;
125109
} else {
126110
const map = this.session.get(filePath);
127111
if (!map) {

‎src/SyntaxThemeDefaults.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ export const SYNTAX_TOKEN_GROUPS: Record<string, string[]> = {
88
Other: ['regex', 'url', 'char', 'symbol', 'important', 'bold', 'italic', 'prolog', 'cdata'],
99
};
1010

11-
export const ALL_SYNTAX_TOKENS: string[] = Object.values(SYNTAX_TOKEN_GROUPS).flat();
12-
1311
export const SYNTAX_TOKEN_DISPLAY_NAMES: Record<string, string> = {
1412
'keyword': 'Keyword',
1513
'string': 'String',

‎src/Utils.ts‎

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,34 +1420,6 @@ export async function addTextToClipboard(content: string) {
14201420
}
14211421
}// addTextToClipboard
14221422

1423-
export function getTextValues(rawText: string) {
1424-
let pipedText, displayText, linkText;
1425-
if (rawText.includes("|")) {
1426-
pipedText = extractText(rawText);
1427-
if (typeof pipedText === 'string') {
1428-
displayText = pipedText;
1429-
linkText = pipedText;
1430-
} else {
1431-
displayText = pipedText.after;
1432-
linkText = pipedText.before;
1433-
}
1434-
} else {
1435-
displayText = rawText;
1436-
linkText = rawText;
1437-
}
1438-
1439-
return { displayText, linkText };
1440-
}// getTextValues
1441-
1442-
function extractText(input: string): { before: string, after: string } | string {
1443-
if (input.includes('|')) {
1444-
const [before, after] = input.split('|');
1445-
return { before, after };
1446-
} else {
1447-
return input;
1448-
}
1449-
}// extractText
1450-
14511423
export function getLanguageSpecificColorClass(codeblockLanguage: string, languageSpecificColors: Record<string, Record<string, string>> | null, languageSpecificColor?: Record<string, string>) {
14521424
let codeblockLanguageSpecificClass = "";
14531425
const language = codeblockLanguage.length > 0 ? codeblockLanguage : "nolang";
@@ -1470,12 +1442,6 @@ export function getLanguageSpecificColorClass(codeblockLanguage: string, languag
14701442
return codeblockLanguageSpecificClass;
14711443
}// getLanguageSpecificColorClass
14721444

1473-
export function getValueNameByLineNumber(lineNumber: number, altLineSpecificWords: { name: string; lineNumber: number; value?: string }[]): { extractedValues: { value: string | undefined, name: string }[] } {
1474-
const matchingItems = altLineSpecificWords.filter(item => item.lineNumber === lineNumber);
1475-
const extractedValues = matchingItems.map(item => ({ value: item.value, name: item.name }));
1476-
return { extractedValues };
1477-
}// getValueNameByLineNumber
1478-
14791445
export function findAllOccurrences(mainString: string, substring: string): number[] {
14801446
const indices: number[] = [];
14811447
let currentIndex = mainString.indexOf(substring);
@@ -1488,20 +1454,6 @@ export function findAllOccurrences(mainString: string, substring: string): numbe
14881454
return indices;
14891455
}// findAllOccurrences
14901456

1491-
export function removeFirstLine(inputString: string): string {
1492-
const lines = inputString.split('\n');
1493-
1494-
if (lines.length > 1) {
1495-
const modifiedLines = lines.slice(1);
1496-
const resultString = modifiedLines.join('\n');
1497-
1498-
return resultString;
1499-
} else {
1500-
// If there's only one line or the input is empty, return an empty string
1501-
return '';
1502-
}
1503-
}// removeFirstLine
1504-
15051457
export function mergeBorderColorsToLanguageSpecificColors(app: CodeBlockCustomizerPlugin, settings: CodeblockCustomizerSettings) {
15061458
const borderColors = settings.SelectedTheme.colors[getCurrentMode()].codeblock.languageBorderColors;
15071459

0 commit comments

Comments
 (0)