Skip to content

Commit

Permalink
update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jgerigmeyer committed Jun 22, 2023
1 parent c4f4bef commit 07a1399
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
7 changes: 3 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ npm run lint
while `render` always parses the partial as a LiquidJS template.
- `.md` files are parsed both as Markdown _and_ as LiquidJS templates.
- When using Markdown, remember that _indentation and whitespace (e.g newlines)
matter_. Use Markdown selectively, especially in files that include other
non-Markdown partials.
- For example, the `{% codeExample %}` tag renders whitespace that results
in unwanted `<p>` tags when parsed as Markdown.
matter_. Custom tags attempt to strip leading whitespace from text contents
(based on the whitespace of the first line) to allow for indented code blocks
-- see the `stripIndent` function in `source/helpers/type.ts` for details.

## Deploying

Expand Down
4 changes: 0 additions & 4 deletions source/helpers/components/codeExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ import {stripIndent} from '../type';
* If `syntax` is either `sass` or `scss`, the first section will be
* interpreted as that syntax and the second will be interpreted (or
* auto-generated) as the CSS output.
*
* Note that this template includes whitespace that renders unwanted extra `<p>`
* tags when parsed as Markdown. To avoid this, ensure that any usage of
* `{% codeExample %}` is *not* within a Markdown file or block.
*/
export default async function codeExample(
contents: string,
Expand Down
11 changes: 6 additions & 5 deletions source/helpers/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ export const getLorem = (type: string, number = 1) => {
* @see https://github.com/jamiebuilds/min-indent
*/
export const stripIndent = (contents: string) => {
// Strip leading whitespace based on line with least leading whitespace
let text = contents;
// Find leading whitespace of first line (ignoring initial newlines)
const match = /^[\n\r]*([ \t]*)(?=\S)/.exec(text);
const match = /^[\n\r]*([ \t]*)(?=\S)/.exec(contents);
if (match?.[1]?.length) {
// Strip leading whitespace based on first line
text = text.replaceAll(new RegExp(`^[ \\t]{${match[1].length}}`, 'gm'), '');
return contents.replaceAll(
new RegExp(`^[ \\t]{${match[1].length}}`, 'gm'),
''
);
}
return text;
return contents;
};

/**
Expand Down

0 comments on commit 07a1399

Please sign in to comment.