Skip to content

Commit

Permalink
Remove Obsidian Links
Browse files Browse the repository at this point in the history
  • Loading branch information
otaviocc committed May 1, 2024
1 parent a60196c commit 41732bf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/extensions/String.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
declare global {
export interface String {
removeFrontmatter(): string
removeObsidianLinks(): string
validValues(): string[]
}
}
Expand All @@ -17,4 +18,49 @@ String.prototype.validValues = function(this: string) {
.map(tag => tag.trim())
}

interface Bounds {
start: number
end: number
}

String.prototype.removeObsidianLinks = function (this: string) {
const linkRegex = /\[\[([^\[\]]+)\]\]/g
const codeBlockRegex = /```[\s\S]*?```/g
const inlineCodeRegex = /`[^`]+`/g

const extractBounds = (regex: RegExp): Bounds[] => {
const bounds: Bounds[] = []
let match

while ((match = regex.exec(this)) !== null) {
bounds.push({
start: match.index,
end: match.index + match[0].length
})
}

return bounds
}

const codeBlocksBounds = extractBounds(codeBlockRegex)
const inlineCodesBounds = extractBounds(inlineCodeRegex)

const cleanMarkdown = (match: string, content: string, index: number): string => {
const isInCodeBlock = codeBlocksBounds.some(
(block) => index >= block.start && index <= block.end
)
const isInInlineCode = inlineCodesBounds.some(
(code) => index >= code.start && index <= code.end
)

if (isInCodeBlock || isInInlineCode) {
return match
} else {
return content.includes('|') ? content.split('|')[1] : content
}
}

return this.replace(linkRegex, cleanMarkdown)
}

export { }
1 change: 1 addition & 0 deletions src/models/MarkdownPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class MarkdownPage implements MarkdownPageInterface {
return this.markdownView.editor
.getValue()
.removeFrontmatter()
.removeObsidianLinks()
}

public get url(): string | null {
Expand Down
1 change: 1 addition & 0 deletions src/models/MarkdownPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class MarkdownPost implements MarkdownPostInterface {
return this.markdownView.editor
.getValue()
.removeFrontmatter()
.removeObsidianLinks()
}

public get tags(): string | null | undefined {
Expand Down

0 comments on commit 41732bf

Please sign in to comment.