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

Normalize Whitespace Plugin: add configuration via attributes #3467

Merged

Conversation

plasticrake
Copy link
Contributor

Added the ability to set configuration using data- attributes. Previously you could only set them via page-wide defaults.

It appeared there was some unused code with env.settings left over from PR #851. But without that PR/plugin you cannot set anything at the block-level.

Original Plugin Author: @zeitgeist87

@github-actions
Copy link

github-actions bot commented Jun 9, 2022

JS File Size Changes (gzipped)

A total of 1 files have changed, with a combined diff of +180 B (+16.5%).

file master pull size diff % diff
plugins/normalize-whitespace/prism-normalize-whitespace.min.js 1.09 KB 1.27 KB +180 B +16.5%

Generated by 🚫 dangerJS against 1d5d896

Copy link
Member

@RunDevelopment RunDevelopment left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR @plasticrake!

I think the idea is good, but we need to improve the implementation a little.

plugins/normalize-whitespace/prism-normalize-whitespace.js Outdated Show resolved Hide resolved
plugins/normalize-whitespace/index.html Show resolved Hide resolved
// value, settingType
assert(t('', 'boolean') === true);
assert(t('true', 'boolean') === true);
assert(t('false', 'boolean') === false);
assert(t('invalid', 'boolean') === undefined);

assert(t('', 'number') === undefined);
assert(t('0', 'number') === 0);
assert(t('123', 'number') === 123);
assert(t('invalid', 'number') === undefined);
assert(t('123invalid456', 'number') === undefined);
@plasticrake
Copy link
Contributor Author

It is more strict parsing data-props. If a value is unexpected it is ignored. Boolean props set with <pre data-prop> are set to true.

const assert = require('assert').strict;

function t (value, settingType) {
  switch (settingType) {
    case 'boolean':
      value = (value === 'true' || value === '') || (value === 'false' ? false : undefined);
      break;
    case 'number':
      value = (value === '' ? undefined : (isNaN(+value) ? undefined : +value));
      break;
  }
  return value;
}

assert(t('', 'boolean') === true);
assert(t('true', 'boolean') === true);
assert(t('false', 'boolean') === false);
assert(t('invalid', 'boolean') === undefined);

assert(t('', 'number') === undefined);
assert(t('0', 'number') === 0);
assert(t('123', 'number') === 123);
assert(t('invalid', 'number') === undefined);
assert(t('123invalid456', 'number') === undefined);

plasticrake and others added 2 commits June 10, 2022 09:21
Co-authored-by: Michael Schmidt <msrd0000@gmail.com>
@RunDevelopment RunDevelopment merged commit 91dea0c into PrismJS:master Jun 10, 2022
@RunDevelopment
Copy link
Member

Thank you for contributing @plasticrake!

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

Successfully merging this pull request may close these issues.

None yet

2 participants