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

Need an example #3

Closed
talatkuyuk opened this issue Feb 10, 2024 · 2 comments
Closed

Need an example #3

talatkuyuk opened this issue Feb 10, 2024 · 2 comments

Comments

@talatkuyuk
Copy link

talatkuyuk commented Feb 10, 2024

I need a help.

I have a test file. I created a compiler and a process function so as to use in the tests. Here is simplified version of my code.

I couldn't figure out where to put use(unifiedPrettier) for specific to that example in order to prettify the output? I tried but got different errors.

const compiler = unified()
  .use(remarkParse)
  .use(gfm)
  .use(remarkRehype, { allowDangerousHtml: true })
  .use(rehypeRaw)
  .use(rehypeStringify);

const process = async (contents: VFileCompatible): Promise<VFileCompatible> => {
  return compiler.process(contents).then((file) => file.value);
};

it("pass", async () => {
  expect(await process("Hi")).toBe("<p>Hi</p>");
});
@remcohaszing
Copy link
Owner

unified-prettier should go anywhere after the plugin that registers a compiler. So in your case, it should go after rehype-stringify.

Prettier options are resolved based on the file path. So you need to pass that too. Your code should become something like this:

const compiler = unified()
  .use(remarkParse)
  .use(gfm)
  .use(remarkRehype, { allowDangerousHtml: true })
  .use(rehypeRaw)
  .use(rehypeStringify)
  .use(unifiedPrettier);

const process = async (contents: VFileCompatible): Promise<VFileCompatible> => {
  return compiler.process(contents).then((file) => file.value);
};

it("pass", async () => {
  expect(await process({ path: 'output.html', value: "Hi" })).toBe("<p>Hi</p>");
});

The intention of this plugin is to work with unified-engine. It could work for programmatic use, but you are probably better off passing the output to Prettier yourself. So I could make some changes to better cater this project to your needs, but I doubt it has much added value.

remcohaszing added a commit that referenced this issue Feb 12, 2024
@remcohaszing
Copy link
Owner

I’m closing this, because there are better ways to use Prettier programmatically.

@remcohaszing remcohaszing closed this as not planned Won't fix, can't repro, duplicate, stale Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants