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

Add support for width in html output #362

Closed
wants to merge 1 commit into from

Conversation

Its-Just-Nans
Copy link

@Its-Just-Nans Its-Just-Nans commented Jan 26, 2024

Hello,

Here is a tiny PR to add --width support in HTML

@kivikakk
Copy link
Owner

Hi there! Thank you for your work here.

Unfortunately I will not accept this PR — I think it would be considered very surprising that setting width would cause the output to change from an HTML fragment to an entire HTML document with added CSS.

The existing width option is documented as such:

/// The wrap column when outputting CommonMark.

In general, styling of the result is left to the end-user, since everyone will want something different. Someone else might want or assume that width is measured in px instead of %. Another might not want the margin: auto centring.

Comrak's scope is converting CommonMark to a given output format; things that are completely outside of that scope should be left out of it. Seeing as there is no provision in CommonMark itself to e.g. add a document title (such as would appear in a <head><title>…</title></head>), or produce output that is horizontally-centred and filling a certain width of the display, Comrak itself shouldn't try to get involved with those ideas.

In other words, if you want such a header and footer, and if you want to apply a width: x% CSS rule to the body, you should do that after receiving the output from Comrak, in your own script/library/program.

(The existing width option only applies to CommonMark output since there's no other way to reliably wrap such output without involving a CommonMark processor. If we ported upstream's plaintext/LaTeX/manpage modes, they'd also honour it.)

Here's a shell script which produces output like you want using Comrak. The benefit is that you don't need a new release every time you want to change the styles.

#!/usr/bin/env sh

body_tag="<body>"
if test -n "$WIDTH"; then
  body_tag="<body style=\"width: $WIDTH%; margin: auto;\">"
fi

cat <<HTML
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
$body_tag
$(comrak "$@")
</body>
</html>
HTML

You can invoke it like Comrak regularly, except if the WIDTH environment variable is set, it'll adjust the <body> tag's style accordingly.

@kivikakk kivikakk closed this Feb 23, 2024
@Its-Just-Nans
Copy link
Author

I thought after making the PR
100 % understandable

Thanks for the very detailled answer !

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

Successfully merging this pull request may close these issues.

2 participants