-
Notifications
You must be signed in to change notification settings - Fork 75
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
🧮 Numbering improvements for custom containers #790
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'myst-frontmatter': patch | ||
'myst-directives': patch | ||
'myst-transforms': patch | ||
--- | ||
|
||
Allow new numbering options to filter through. Add new `kind` option to figure directive |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,39 +3,67 @@ import { | |
defined, | ||
incrementOptions, | ||
validateBoolean, | ||
validateObjectKeys, | ||
validateKeys, | ||
validateObject, | ||
validateString, | ||
} from 'simple-validators'; | ||
import type { Numbering } from './types.js'; | ||
|
||
export const NUMBERING_KEYS = [ | ||
'enumerator', | ||
'figure', | ||
'equation', | ||
'table', | ||
'code', | ||
'heading_1', | ||
'heading_2', | ||
'heading_3', | ||
'heading_4', | ||
'heading_5', | ||
'heading_6', | ||
]; | ||
export const NUMBERING_OPTIONS = ['enumerator', 'headings']; | ||
|
||
const HEADING_KEYS = ['heading_1', 'heading_2', 'heading_3', 'heading_4', 'heading_5', 'heading_6']; | ||
export const NUMBERING_KEYS = ['figure', 'equation', 'table', 'code', ...HEADING_KEYS]; | ||
|
||
export const NUMBERING_ALIAS = { | ||
sections: 'headings', | ||
h1: 'heading_1', | ||
h2: 'heading_2', | ||
h3: 'heading_3', | ||
h4: 'heading_4', | ||
h5: 'heading_5', | ||
h6: 'heading_6', | ||
heading1: 'heading_1', | ||
heading2: 'heading_2', | ||
heading3: 'heading_3', | ||
heading4: 'heading_4', | ||
heading5: 'heading_5', | ||
heading6: 'heading_6', | ||
}; | ||
|
||
/** | ||
* Validate Numbering object | ||
*/ | ||
export function validateNumbering(input: any, opts: ValidationOptions): Numbering | undefined { | ||
const value = validateObjectKeys(input, { optional: NUMBERING_KEYS }, opts); | ||
const obj = validateObject(input, opts); | ||
if (obj === undefined) return undefined; | ||
const value = validateKeys( | ||
obj, | ||
{ optional: [...NUMBERING_KEYS, ...NUMBERING_OPTIONS], alias: NUMBERING_ALIAS }, | ||
// Do not add warnings on this filter process | ||
{ property: '', messages: { errors: [], warnings: [] }, keepExtraKeys: true }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in #794. |
||
); | ||
if (value === undefined) return undefined; | ||
const output: Record<string, any> = {}; | ||
if (defined(value.enumerator)) { | ||
output.enumerator = validateString(value.enumerator, incrementOptions('enumerator', opts)); | ||
} | ||
NUMBERING_KEYS.filter((key) => key !== 'enumerator').forEach((key) => { | ||
if (defined(value[key])) { | ||
output[key] = validateBoolean(value[key], incrementOptions(key, opts)); | ||
} | ||
}); | ||
if (defined(value.headings)) { | ||
const headings = validateBoolean(value.headings, incrementOptions('headings', opts)); | ||
HEADING_KEYS.forEach((headingKey) => { | ||
if (headings && !defined(value[headingKey])) { | ||
// This will be validated next! | ||
value[headingKey] = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
}); | ||
} | ||
Object.keys(value) | ||
.filter((key) => !NUMBERING_OPTIONS.includes(key)) // For all the unknown options | ||
.forEach((key) => { | ||
if (defined(value[key])) { | ||
const bool = validateBoolean(value[key], incrementOptions(key, opts)); | ||
if (defined(bool)) output[key] = bool; | ||
} | ||
}); | ||
if (Object.keys(output).length === 0) return undefined; | ||
return output; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And then I guess this just works with the
enumerate
transform, since it already increments count based onkind
. Nice.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was impressed at how much of this already just worked and all we needed to do was to be less strict in validation!