mirror of
https://github.com/kdnk/obsidian-automatic-linker.git
synced 2026-07-22 05:37:46 +00:00
fix: preserve frontmatter URL formatting
Why: The Task 2 refactor split frontmatter before URL rewriting, which left GitHub/Jira/Linear URLs in YAML raw even though document formatting previously handled them. What: Add a regression test for frontmatter URL formatting with body-only title/link behavior, factor URL rewriting into a shared helper, and apply it to the original frontmatter slice before body-only formatting.
This commit is contained in:
parent
6585b7393a
commit
9098a48053
2 changed files with 57 additions and 11 deletions
|
|
@ -55,6 +55,39 @@ describe("formatMarkdownDocument", () => {
|
|||
)
|
||||
})
|
||||
|
||||
it("formats GitHub URLs in frontmatter without moving body-only formatting into frontmatter", () => {
|
||||
const { candidateMap, trie } = buildCandidateTrieForTest({
|
||||
files: [{ path: "notes/TypeScript" }],
|
||||
settings: {
|
||||
scoped: false,
|
||||
baseDir: undefined,
|
||||
ignoreCase: true,
|
||||
},
|
||||
})
|
||||
|
||||
const result = formatMarkdownDocument({
|
||||
content:
|
||||
"---\nautomatic-linker-disable-url-title: true\nreference: https://github.com/openai/openai/issues/1\nterm: TypeScript\n---\nTypeScript https://example.com",
|
||||
filePath: "current-file.md",
|
||||
frontmatter: {
|
||||
"automatic-linker-disable-url-title": true,
|
||||
},
|
||||
settings: {
|
||||
...DEFAULT_SETTINGS,
|
||||
formatGitHubURLs: true,
|
||||
formatJiraURLs: false,
|
||||
formatLinearURLs: false,
|
||||
replaceUrlWithTitle: true,
|
||||
ignoreCase: true,
|
||||
},
|
||||
candidateIndex: { candidateMap, trie },
|
||||
})
|
||||
|
||||
expect(result).toBe(
|
||||
"---\nautomatic-linker-disable-url-title: true\nreference: [[github/openai/openai/issues/1]] [🔗](https://github.com/openai/openai/issues/1)\nterm: TypeScript\n---\n[[notes/TypeScript|TypeScript]] https://example.com",
|
||||
)
|
||||
})
|
||||
|
||||
it("runs URL formatting, URL titles, and link replacement in order", () => {
|
||||
const { candidateMap, trie } = buildCandidateTrieForTest({
|
||||
files: [{ path: "notes/TypeScript" }],
|
||||
|
|
|
|||
|
|
@ -44,6 +44,25 @@ export const toReplaceLinksSettings = (
|
|||
ignoreMarkdownTables: settings.ignoreMarkdownTables,
|
||||
})
|
||||
|
||||
const formatMarkdownURLs = (
|
||||
text: string,
|
||||
settings: AutomaticLinkerSettings,
|
||||
): string => {
|
||||
let updatedText = text
|
||||
|
||||
if (settings.formatGitHubURLs) {
|
||||
updatedText = replaceURLs(updatedText, settings, formatGitHubURL)
|
||||
}
|
||||
if (settings.formatJiraURLs) {
|
||||
updatedText = replaceURLs(updatedText, settings, formatJiraURL)
|
||||
}
|
||||
if (settings.formatLinearURLs) {
|
||||
updatedText = replaceURLs(updatedText, settings, formatLinearURL)
|
||||
}
|
||||
|
||||
return updatedText
|
||||
}
|
||||
|
||||
export const formatMarkdownBody = ({
|
||||
body,
|
||||
filePath,
|
||||
|
|
@ -54,17 +73,8 @@ export const formatMarkdownBody = ({
|
|||
urlTitleMap = new Map(),
|
||||
linkGenerator,
|
||||
}: Omit<FormattingRunOptions, "content"> & { body: string }): string => {
|
||||
let updatedBody = body
|
||||
let updatedBody = formatMarkdownURLs(body, settings)
|
||||
|
||||
if (settings.formatGitHubURLs) {
|
||||
updatedBody = replaceURLs(updatedBody, settings, formatGitHubURL)
|
||||
}
|
||||
if (settings.formatJiraURLs) {
|
||||
updatedBody = replaceURLs(updatedBody, settings, formatJiraURL)
|
||||
}
|
||||
if (settings.formatLinearURLs) {
|
||||
updatedBody = replaceURLs(updatedBody, settings, formatLinearURL)
|
||||
}
|
||||
if (settings.replaceUrlWithTitle && !isUrlTitleReplacementOff(frontmatter)) {
|
||||
updatedBody = replaceUrlWithTitle({ body: updatedBody, urlTitleMap })
|
||||
}
|
||||
|
|
@ -118,7 +128,10 @@ export const formatMarkdownDocument = ({
|
|||
contentStart = inferContentStart(content),
|
||||
...options
|
||||
}: FormattingRunOptions): string => {
|
||||
const frontmatterText = content.slice(0, contentStart)
|
||||
const frontmatterText = formatMarkdownURLs(
|
||||
content.slice(0, contentStart),
|
||||
options.settings,
|
||||
)
|
||||
const body = content.slice(contentStart)
|
||||
return frontmatterText + formatMarkdownBody({ ...options, body })
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue