Skip to content

Invalid path and filename in Windows #79

@AceTheWiz

Description

@AceTheWiz

Firstly, thanks for the utility it helped me split my large MD files (and learn a bit about Node JS).

I'm posting my solution because I have never done a pull request before.

My MD file has headings like this (note the embedded ':' and '/')

#### AAA-BBB-1100: Some Title / Other Text

My split-md args were:

cli.args[0]="F:/a/b/c/markdownfile.md";
cli.args[1]="#### ";
cli.args[2]="####";
cli.args[3]="F:/a/b/c/";

So after Line 28

title="F:/a/b/c/AAA-BBB-1100: Some Title / Other Text"

The title/path is still ok at this point.
but after Line 29

title="F/a/b/c/AAA-BBB-1100:Some Title / Other Text.md"

The first ':' is removed making the drive field invalid. Remaining ':' are not removed because title.replace() only replaces the first occurrence. The same happens with the space ' ' replacement.
Anothe problem is that the '/' in the ""filename" is not removed also making it an invalid path.

I replaced these two lines...

split-md/src/splitter.js

Lines 28 to 29 in 495101e

title = writePath + array[i].replace(cleanName, "").trim()
title = title.replace(":", "").replace(" ", "") + ".md"

With these 3 lines...

      title = array[i].replace(cleanName, "").trim()
      title = sanitize(title, {replacement: "-"});
      title = writePath + title + ".md"; 

The writePath is only added after the title is cleaned otherwise it messes up the path.
I used the sanitize-filename library.

const sanitize = require('sanitize-filename');

p.s. I prefer leaving the spaces in the filename and replace illegal characters with a dash. '-'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions