obsidianmd / obsidian-clipper

Official web clipper extension for Obsidian.
https://obsidian.md
MIT License
612 stars 23 forks source link

BUG: safe_name filter includes characters invalid for Obsidian name #100

Open esodesod opened 2 days ago

esodesod commented 2 days ago

Not directly a bug, but I (intially) though that safe_name filter supposed to result in a "safe name"? Maybe not limited to (only) OS specific, also produce something that is fully Obsidian compatible? Maybe something like safe_name:obsidian?

Example below

Version (please complete the following information):

Describe the bug Using safe_name filter still produces a name which is not valid as title (file name) in Obsidian, e.g. includes # in file name

To reproduce Steps to reproduce the behavior:

  1. Go to YouTube video for Obsidian October 2024 — Critique your plugin #1! (https://www.youtube.com/watch?v=g2_XEDoPF4s)
  2. Try saving to a note, where it includes the title/video name, which includes # (in this case)

As an example, I'm using: {{schema:@VideoObject:uploadDate|date:"YYYY-MM-DD"}} VIDEO {{schema:@VideoObject:author}} - {{schema:@VideoObject:name|safe_name}}

Which results in something like this in Obsidian (in this case, the # should not be included?) image

Expected behavior A "safe name" that should be able to be used in Obsidian, without any possible issues (based on the name)

Workaround Using replace filter, e.g. {{schema:@VideoObject:uploadDate|date:"YYYY-MM-DD"}} VIDEO {{schema:@VideoObject:author}} - {{schema:@VideoObject:name|safe_name|replace:"\#":"","\!":""|trim}}

Your template file

{
  "schemaVersion": "0.1.0",
  "name": "YouTube with transcript",
  "behavior": "create",
  "noteContentFormat": "## About\n\ntype:: #type/video/youtube/new\n\n![{{title}}]({{url}})\n\n## Description\n\n{{schema:@VideoObject:description}}\n\n## Notes\n\nYT=\n\n\n## Transcript\n\n{{selectorHtml:.ytd-transcript-segment-list-renderer|replace:\" \":\" \"|join|markdown}}",
  "properties": [
    {
      "name": "created",
      "value": "{{time|date:\\\"YYYY-MM-DDTHH:mm:ssZ\\\"}}",
      "type": "datetime"
    },
    {
      "name": "reviewed",
      "value": "",
      "type": "datetime"
    },
    {
      "name": "url",
      "value": "{{url}}",
      "type": "text"
    },
    {
      "name": "title",
      "value": "{{schema:@VideoObject:name}}",
      "type": "text"
    },
    {
      "name": "channel",
      "value": "{{schema:@VideoObject:author}}",
      "type": "text"
    },
    {
      "name": "related",
      "value": "[[Videos]]",
      "type": "text"
    },
    {
      "name": "published",
      "value": "{{schema:@VideoObject:uploadDate|date:\\\"YYYY-MM-DD\\\"}}",
      "type": "datetime"
    },
    {
      "name": "duration",
      "value": "{{schema:@VideoObject:duration|replace:\\\"PT\\\",\\\"\\\",\\\"S\\\",\\\"\\\"}}",
      "type": "text"
    },
    {
      "name": "watched",
      "value": "",
      "type": "text"
    }
  ],
  "triggers": [],
  "noteNameFormat": "{{schema:@VideoObject:uploadDate|date:\"YYYY-MM-DD\"}} VIDEO {{schema:@VideoObject:author}} - {{schema:@VideoObject:name|safe_name|replace:\"\\#\":\"\",\"\\!\":\"\"|trim}}",
  "path": ""
}
kepano commented 1 day ago

To clarify the issue here, the problem is only with the # character, or have you encountered others?

esodesod commented 1 day ago

Well, I only encountered the # character, but now you got me curious if I could test the remaining "invalid" (Obsidian) characters as well :-).

I really don't know if this is the correct approach to "hard code a title" in combination with filters, but if I test using something like {{title|replace:"app":"NewApp #1!^[]|"|safe_name}} for note name, and using your post https://stephango.com/file-over-app, it seems to still produce all the invalid characters with the exception of | being "sanitized" (EDIT: but maybe that needs escaping?). I'm no coder/developer, but by the look of the regex in safe_filter, I guess it's done here somewhere? https://github.com/obsidianmd/obsidian-clipper/blob/bea800d5894876e6ef2bc0a9c57d4b443dc5e66a/src/utils/filters/safe_name.ts#L26

Might just be a "personal preference" (in my case), but I'd like a filter (or safe_name) to be able to produce a note name that is "safe to use in Obsidian"; meaning all characters that possibly could produce issues within Obsidian are sanitized, if that makes sense.

image image