Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown plugin fails to parse metadata when given a file with CR or CRLF line endings #98

Closed
StJohn3D opened this issue Aug 7, 2019 · 8 comments
Labels

Comments

@StJohn3D
Copy link

StJohn3D commented Aug 7, 2019

Given a file _nav.md

---
menu: [
  {
    icon: faHome,
    page: Home
  }
]
---
Don't delete this text,
this file has to contain something besides metaData for it to be parsed by documentalist.

The metadata will only be parsed if the file has Unix LF line endings. If the file has Windows CR LF or Mac CR it will fail to parse the metadata.

@adidahiya
Copy link
Contributor

Are you using Mac or Windows for these various files with different line endings? We simply use fs.readFileSync(path, "utf8") to read markdown files, so as long as you use the standard convention on the OS, it should work fine...

@StJohn3D
Copy link
Author

@adidahiya
People using macs don't have this issue, but people on windows - if they haven't configured git to normalize line endings to LF on checkout, they will get different results. The generated json will just have empty meta-data entries instead of actually capturing the meta-data from the markdown file.

@StJohn3D
Copy link
Author

StJohn3D commented Aug 21, 2019

@adidahiya to test, copy the example above (_nav.md) and open the file in notepad++
Once open in NotePad++

  • click View/Show Symbol/Show All Characters
  • then use Edit/EOL Conversion to test different line endings.
  • save and run the file through documentalist to generate some json

On windows you should end up with a different/wrong json output when using anything besides LF
At least that has been my experience.

@adidahiya
Copy link
Contributor

I doubt we're going to do anything special here for this edge case, but let me know if you have any clever ideas for addressing the problem in a simple way... for now just work around this by normalizing your file endings

@adidahiya adidahiya reopened this Aug 21, 2019
@adidahiya adidahiya added the bug label Aug 21, 2019
@StJohn3D
Copy link
Author

Sure thing, for now this is how I'm working around it.

npm i -D replace-in-file

normalizeLineEndings.js

const replace = require('replace-in-file');

module.exports = (filesGlob) => {
  const options = {
    files: filesGlob,
    from: /\r\n|\r/gm,
    to: '\n',
  };
  try {
    console.log("Normalizing line endings to LF...")
    const results = replace.sync(options);
    const filesChanged = results.filter(r => r.hasChanged).map(f => f.file)
    if (filesChanged.length > 0) console.log('    Normalized the following files:', filesChanged);
    console.log("done.")
  }
  catch (error) {
    console.error('Error occurred:', error);
  }
}

Then wherever you use documentalist

const normalizeLineEndings = require('./path/to/normalizeLineEndings')
// ...
normalizeLineEndings('src/**/*.md')
new Documentalist()
// ...

@egoroof
Copy link

egoroof commented Mar 10, 2020

Face the same problem

This is my solution:

const { Documentalist, MarkdownPlugin, TypescriptPlugin } = require('@documentalist/compiler');
const { writeFileSync } = require('fs');

class LineFormatter {
    compile(files) {
        for (const file of files) {
            writeFileSync(file.path, file.read().replace(/\r\n/g, '\n'));
        }
    }
}

new Documentalist()
    .use('.md', new LineFormatter())
    .use('.md', new MarkdownPlugin())
    .use(/\.ts$/, new TypescriptPlugin({ excludePaths: ['src'] }))
    .documentGlobs('sdk/**/*')
    .then((docs) => JSON.stringify(docs))
    .then((json) => writeFileSync('dist/docs.json', json))
    .catch((e) => {
        console.log(e);
        process.exit(1);
    });

But this should definitly do MarkdownPlugin itself

@bvandercar-vt
Copy link

Face the same problem

This is my solution:

const { Documentalist, MarkdownPlugin, TypescriptPlugin } = require('@documentalist/compiler');
const { writeFileSync } = require('fs');

class LineFormatter {
    compile(files) {
        for (const file of files) {
            writeFileSync(file.path, file.read().replace(/\r\n/g, '\n'));
        }
    }
}

new Documentalist()
    .use('.md', new LineFormatter())
    .use('.md', new MarkdownPlugin())
    .use(/\.ts$/, new TypescriptPlugin({ excludePaths: ['src'] }))
    .documentGlobs('sdk/**/*')
    .then((docs) => JSON.stringify(docs))
    .then((json) => writeFileSync('dist/docs.json', json))
    .catch((e) => {
        console.log(e);
        process.exit(1);
    });

But this should definitly do MarkdownPlugin itself

This worked for me! Except to avoid actually writing the files, did

class LineFormatter {
    compile(files) {
        return files.map(file => file.read().replace(/\r\n/g, "\n"));
    }
}

@bvandercar-vt
Copy link

Inline:

.use(".md", {
            compile: files =>
                process.platform === "win32" ? files.map(file => file.read().replace(/\r\n/g, "\n")) : files,
        })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants