Enveloppe / obsidian-enveloppe

Enveloppe helps you to publish your notes on a GitHub repository from your Obsidian Vault, for free!
https://enveloppe.github.io/
Apache License 2.0
625 stars 39 forks source link

[Plugin][FR]: Remove .md extension from generated links #176

Closed quinn-p-mchugh closed 1 year ago

quinn-p-mchugh commented 1 year ago

Issue validation

Is your feature related to a problem ?

I recently confirmed that Docusaurus does not support relative markdown linking between blog and doc pages.

This means that the relative markdown links ([link](../../docs/.md#) ) generated by the Obsidian GitHub Publisher plugin do not work for links between pages in the blog section and the docs section any Docusaurus website. This might be fixed in the future, but one of the lead developer's notes that this would require a significant change to how links are routed in Docusaurus.

However, using standard URL links (without the .md extension) work just fine.

For example: If I provide a link in a blog post like [link](../../docs/relevant-doc-page.md#), the link does not work.

whereas...

When I remove the .md extension ([link](../../docs/relevant-doc-page)), the link routes successfully.

What solution do you want to see ?

As a workaround, I'd like an option to remove the .md extension from all wikilinks that are converted to relative markdown links, so these links work properly on websites built with Docusaurus.

So instead of: [link](../../docs/relative-doc-page.md#)

It should be: [link](../../docs/relative-doc-page)

This could be a matter of including the file extension in any user-specified regular expressions in the Apply edit on the folder path or filename (automatically) setting, allowing users to remove the .md file extension by specifying .md under Value to replace and nothing for the Replacement value.

Describe the alternative you've considered

Other solutions might look like:

OS

Windows

Anything else?

I think enabling the ability to modify file extensions (in addition to filename and folder path) in the Apply edit on the folder path or filename (automatically) setting would be the most flexible solution. However, this may or may not cause issues in the event that users' existing regex replace settings happen to match the .md file extension.

Plugin version

6.2.5

Obsidian version & debug log

N/A
Mara-Li commented 1 year ago

Real question: did you try to use the regex replacement of contents to remove the .MD extension?

Probably something with \((.*)\.md(.*)\)($1$2)

See https://regex101.com/r/2xMqDG/1

You can remove the # part with juste using ($1) as replacement.

quinn-p-mchugh commented 1 year ago

Hi @Lisandra-dev,

Great question - this may very well be a case of user error!

I'll give the above suggestion a try, but given that use of backslashes yields an error (see #172), I'll need to wait until the new release to begin testing.

Thanks for your prompt response! Very much appreciate the help here.

quinn-p-mchugh commented 1 year ago

FWIW, I've confirmed that the following (non-regex) replacement does not remove the .md extension.

image

Also: thank you for filling in the issue title - I neglected to do this by mistake. My apologies.

Mara-Li commented 1 year ago

As I said in my first post, you need to set the regex in the content remplacement. Not in the path remplacement.

quinn-p-mchugh commented 1 year ago

Hi @Lisandra-dev,

Ah, thank you - I'll read more carefully next time.

Hmmm... it seems the following settings in the Text replacer setting do not remove the .md extension from links. image

I've verified convertWikiLinks function is called before the findAndReplaceText function, so no issues there.

I might have to do some debugging on my end to see what might be going awry.

In any case, here's my settings and test content, in case anything jumps out at you.

Settings

{
  "github": {
    "branch": "main",
    "automaticallyMergePR": true,
    "tokenPath": "%configDir%/plugins/%pluginID%/env",
    "api": {
      "tiersForApi": "Github Free/Pro/Team (default)",
      "hostname": ""
    },
    "workflow": {
      "commitMessage": "[Obsidian GitHub Publisher] Merge",
      "name": ""
    },
    "verifiedRepo": true
  },
  "upload": {
    "behavior": "yaml",
    "defaultName": "docs",
    "rootFolder": "",
    "yamlFolderKey": "publish-path",
    "frontmatterTitle": {
      "enable": true,
      "key": "publish-filename"
    },
    "replaceTitle": [
      {
        "regex": "/\\s+/",
        "replacement": "-",
        "type": "title"
      }
    ],
    "replacePath": [],
    "autoclean": {
      "enable": false,
      "excluded": []
    },
    "folderNote": {
      "enable": false,
      "rename": "index.md"
    },
    "metadataExtractorPath": ""
  },
  "conversion": {
    "hardbreak": false,
    "dataview": true,
    "censorText": [
      {
        "entry": "/\\.md/",
        "replace": "//",
        "after": false
      },
      {
        "entry": ".md",
        "replace": "",
        "after": false
      },
      {
        "entry": "/\\((.*)\\.md(.*)\\)/",
        "replace": "/$1$2/",
        "after": false
      },
      {
        "entry": "/\\.md/",
        "replace": "",
        "after": false
      }
    ],
    "tags": {
      "inline": false,
      "exclude": [],
      "fields": []
    },
    "links": {
      "internal": true,
      "unshared": false,
      "wiki": true,
      "slugify": true
    }
  },
  "embed": {
    "attachments": true,
    "keySendFile": [],
    "notes": true,
    "folder": ""
  }
}

Test file contents (In Obsidian):

test-blog-post.md

---
publish: true
publish-path: blog/2022
publish-filename: 
---
[[test-docs-post]]

test-docs-post.md

---
publish: true
publish-path: docs
publish-filename: 
---
[[test-blog-post]]

Result (In GitHub)

test-blog-post.md

---
publish: true
publish-path: blog/2022
publish-filename: 
---
[test-docs-post](../../docs/test-docs-post.md#)

test-docs-post.md

---
publish: true
publish-path: docs
publish-filename: 
---
[test-blog-post](../blog/2022/test-blog-post.md#)
Mara-Li commented 1 year ago

Change the arrow to down :)

quinn-p-mchugh commented 1 year ago

Ah, shoot. Thank you @Lisandra-dev! Changing the arrow to down resolved my issue.

I've provided the full resolution below for anyone with similar inquiries.

Solution (to remove .md from markdown links)

The markdown links generated by Obsidian GitHub publisher should now omit the .md extension while maintaining any anchor links (link#my-heading). This should resolve cross-section (blog --> docs) linking issues with Docusarus.

abbychau commented 1 year ago

image

It seems it the links in dataview are not being replaced. can anyone verify?

Mara-Li commented 1 year ago

What do you mean by the links in dataview ?

abbychau commented 1 year ago

Tables in dataview have a link called File, which is a link.

Mara-Li commented 1 year ago

image

No problem on my side :/

Here my settings tested:

 "conversion": {
    "hardbreak": false,
    "dataview": true,
    "censorText": [
      {
        "entry": "/(\\[.*?\\]\\(.*?).md(.*?\\))/",
        "replace": "$1$2",
        "flags": "",
        "after": true
      }
    ],
    "tags": {
      "inline": false,
      "exclude": [],
      "fields": []
    },
    "links": {
      "internal": true,
      "unshared": true,
      "wiki": true,
      "slugify": false
    }
abbychau commented 1 year ago

mine is this:

  "conversion": {
    "hardbreak": true,
    "dataview": true,
    "censorText": [
      {
        "entry": "/(\\[.*?\\]\\(.*?).md(.*?\\))/",
        "replace": "$1$2",
        "flags": "",
        "after": true
      }
    ],
    "tags": {
      "inline": false,
      "exclude": [],
      "fields": []
    },
    "links": {
      "internal": true,
      "unshared": false,
      "wiki": true,
      "slugify": true
    }
  },

in your screenshot, it seems that only the first row of the table was replaced but other links are still with .md is that right?

Mara-Li commented 1 year ago

Oh, yeah, I think you need to use g in the regex. Like that: /(\[.*?\]\(.*?).md(.*?\))/g

abbychau commented 1 year ago

I have added g at the end. but no matter if it is added or unadded. .md is still not being replaced.

Mara-Li commented 1 year ago

image Works on my side :/

abbychau commented 1 year ago

I rebuilt my repo and it is working now. thanks. (the plugin was not pushing changes after editing at all.