lit icon indicating copy to clipboard operation
lit copied to clipboard

insert spaces after inline comments explicitly without awk OFS

Open vijithassar opened this issue 5 years ago • 0 comments

When the --before/-b flags are used to comment out Markdown content, awk prints a space after the character that begins the comment. This is fine when there is a line to comment out:

# let's **create a variable** now
# ```python 
x = 'hello world'
# ```

But awk also inserts the space for line breaks when there is nothing to comment out, which is to say, for line breaks in the Markdown content:

# MY PROGRAM
# 
# let's **create a variable** now
# ```python
x = 'hello world'
# ```

This is happening because awk has a built in variable called OFS, for "output file separator," which is set to the space character by default.

Linters do not like the trailing space on empty comment lines, and often complain about empty space at the end of a line. Whether this problem occurs will depend on your language, your linter, and your specific linter configuration, but prohibiting this in developer tooling is very common.

Instead of relying on the awk OFS variable to supply the space, OFS should be set to an empty string so as not to pad empty comment lines, and then the space can be explicitly added.

This will require measuring the length of the Markdown content being commented out. If the length greater than 0 characters, then the space should be added before the Markdown content is appended to the. If the length of the Markdown content is zero characters, then it's just an empty line, and the space should not be appended.

This is related to the need to generally clean up the awk logic.

vijithassar avatar Nov 16 '18 06:11 vijithassar