Skip to content

Commit

Permalink
refactor: use Obsidian builtin function for link sanitization
Browse files Browse the repository at this point in the history
The Obsidian API now provides stripHeadingForLink() to sanitize heading text for the purposes of creating links. Use this function instead of our custom implementation.
  • Loading branch information
darlal committed Nov 30, 2024
1 parent 60d3b3b commit ba09d9b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 22 deletions.
5 changes: 5 additions & 0 deletions src/__mocks__/obsidian/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
MarkdownRenderer,
prepareFuzzySearch,
prepareSimpleSearch,
stripHeadingForLink,
} from 'obsidian';
import {
MockSetting,
Expand Down Expand Up @@ -86,6 +87,9 @@ const mockRenderResults = mockFn<typeof renderResults>().mockImplementation();
const mockDebounce = mockFn<typeof debounce>().mockImplementation();
const mockSetIcon = mockFn<typeof setIcon>().mockImplementation();
const mockParseLinktext = mockFn<typeof parseLinktext>().mockImplementation();
const mockStripHeadingForLink = mockFn<typeof stripHeadingForLink>().mockImplementation(
(heading) => heading,
);

export {
mockPlatform as Platform,
Expand All @@ -101,6 +105,7 @@ export {
mockNormalizePath as normalizePath,
mockSetIcon as setIcon,
mockParseLinktext as parseLinktext,
mockStripHeadingForLink as stripHeadingForLink,
mockKeymap as Keymap,
mockMarkdownRenderer as MarkdownRenderer,
mockComponent as Component,
Expand Down
16 changes: 0 additions & 16 deletions src/utils/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,22 +308,6 @@ describe('utils', () => {
);
});

test('generated links for Heading subpath should not contain illegal link characters', () => {
const illegalChar = ':#|^\\%%[[]]';
const heading = `head ${illegalChar} tail`;
const expectedLinkHeadingStr = 'head tail';
const sugg = makeHeadingSuggestion(makeHeading(heading, 1), destFile);

generateMarkdownLink(mockFileManager, mockVault, sugg, activeFilePath);

expect(mockFileManager.generateMarkdownLink).toHaveBeenCalledWith(
destFile,
activeFilePath,
`#${expectedLinkHeadingStr}`,
expectedLinkHeadingStr,
);
});

test('with useHeadingAsAlias disabled, it should generate a link for Heading suggestions with file basename as alias', () => {
const heading = chance.sentence();
const sugg = makeHeadingSuggestion(makeHeading(heading, 1), destFile);
Expand Down
8 changes: 2 additions & 6 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Vault,
normalizePath,
WorkspaceLeaf,
stripHeadingForLink,
} from 'obsidian';
import {
SymbolSuggestion,
Expand Down Expand Up @@ -346,12 +347,7 @@ function sanitizeStringForLinkSubpath(
input: string,
useInputAsAlias: boolean,
): { subpath: string; alias: string | null } {
// May 2024: shamelessly borrowed from Obsidian
const illegalLinkCharsRegex = /([:#|^\\\r\n]|%%|\[\[|]])/g;
const sanitizedInput = input
.replace(illegalLinkCharsRegex, ' ')
.replace(/\s+/g, ' ')
.trim();
const sanitizedInput = stripHeadingForLink(input);

return {
subpath: `#${sanitizedInput}`,
Expand Down

0 comments on commit ba09d9b

Please sign in to comment.