-
Notifications
You must be signed in to change notification settings - Fork 73
feat: add frontmatterTitle option to heading-increment #454
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
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 | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,12 +3,21 @@ | |||||||||||||||
* @author Nicholas C. Zakas | ||||||||||||||||
*/ | ||||||||||||||||
|
||||||||||||||||
//----------------------------------------------------------------------------- | ||||||||||||||||
// Imports | ||||||||||||||||
//----------------------------------------------------------------------------- | ||||||||||||||||
|
||||||||||||||||
import { frontmatterHasTitle } from "../util.js"; | ||||||||||||||||
|
||||||||||||||||
//----------------------------------------------------------------------------- | ||||||||||||||||
// Type Definitions | ||||||||||||||||
//----------------------------------------------------------------------------- | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: []; }>} | ||||||||||||||||
* @import { MarkdownRuleDefinition } from "../types.js"; | ||||||||||||||||
* @typedef {"skippedHeading"} HeadingIncrementMessageIds | ||||||||||||||||
* @typedef {[{ frontmatterTitle?: string }]} HeadingIncrementOptions | ||||||||||||||||
* @typedef {MarkdownRuleDefinition<{ RuleOptions: HeadingIncrementOptions, MessageIds: HeadingIncrementMessageIds }>} | ||||||||||||||||
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. JSDoc does not support an
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||
* HeadingIncrementRuleDefinition | ||||||||||||||||
*/ | ||||||||||||||||
|
||||||||||||||||
|
@@ -31,12 +40,52 @@ export default { | |||||||||||||||
skippedHeading: | ||||||||||||||||
"Heading level skipped from {{fromLevel}} to {{toLevel}}.", | ||||||||||||||||
}, | ||||||||||||||||
|
||||||||||||||||
schema: [ | ||||||||||||||||
{ | ||||||||||||||||
type: "object", | ||||||||||||||||
properties: { | ||||||||||||||||
frontmatterTitle: { | ||||||||||||||||
type: "string", | ||||||||||||||||
}, | ||||||||||||||||
}, | ||||||||||||||||
additionalProperties: false, | ||||||||||||||||
}, | ||||||||||||||||
], | ||||||||||||||||
|
||||||||||||||||
defaultOptions: [ | ||||||||||||||||
{ | ||||||||||||||||
frontmatterTitle: | ||||||||||||||||
"^(?!\\s*['\"]title[:=]['\"])\\s*\\{?\\s*['\"]?title['\"]?\\s*[:=]", | ||||||||||||||||
}, | ||||||||||||||||
], | ||||||||||||||||
}, | ||||||||||||||||
|
||||||||||||||||
create(context) { | ||||||||||||||||
const [{ frontmatterTitle }] = context.options; | ||||||||||||||||
const titlePattern = | ||||||||||||||||
frontmatterTitle === "" ? null : new RegExp(frontmatterTitle, "iu"); | ||||||||||||||||
let lastHeadingDepth = 0; | ||||||||||||||||
|
||||||||||||||||
return { | ||||||||||||||||
yaml(node) { | ||||||||||||||||
if (frontmatterHasTitle(node.value, titlePattern)) { | ||||||||||||||||
lastHeadingDepth = 1; | ||||||||||||||||
} | ||||||||||||||||
}, | ||||||||||||||||
|
||||||||||||||||
toml(node) { | ||||||||||||||||
if (frontmatterHasTitle(node.value, titlePattern)) { | ||||||||||||||||
lastHeadingDepth = 1; | ||||||||||||||||
} | ||||||||||||||||
}, | ||||||||||||||||
|
||||||||||||||||
json(node) { | ||||||||||||||||
if (frontmatterHasTitle(node.value, titlePattern)) { | ||||||||||||||||
lastHeadingDepth = 1; | ||||||||||||||||
} | ||||||||||||||||
}, | ||||||||||||||||
|
||||||||||||||||
heading(node) { | ||||||||||||||||
if (lastHeadingDepth > 0 && node.depth > lastHeadingDepth + 1) { | ||||||||||||||||
context.report({ | ||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,14 +7,17 @@ | |||||
// Imports | ||||||
//----------------------------------------------------------------------------- | ||||||
|
||||||
import { findOffsets } from "../util.js"; | ||||||
import { findOffsets, frontmatterHasTitle } from "../util.js"; | ||||||
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. The imported Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
|
||||||
//----------------------------------------------------------------------------- | ||||||
// Type Definitions | ||||||
//----------------------------------------------------------------------------- | ||||||
|
||||||
/** | ||||||
* @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: [{ frontmatterTitle?: string; }]; }>} | ||||||
* @import { MarkdownRuleDefinition } from "../types.js"; | ||||||
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. JSDoc does not recognize
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
* @typedef {"multipleH1"} NoMultipleH1MessageIds | ||||||
* @typedef {[{ frontmatterTitle?: string }]} NoMultipleH1Options | ||||||
* @typedef {MarkdownRuleDefinition<{ RuleOptions: NoMultipleH1Options, MessageIds: NoMultipleH1MessageIds }>} | ||||||
* NoMultipleH1RuleDefinition | ||||||
*/ | ||||||
|
||||||
|
@@ -24,25 +27,6 @@ import { findOffsets } from "../util.js"; | |||||
|
||||||
const h1TagPattern = /(?<!<!--[\s\S]*?)<h1[^>]*>[\s\S]*?<\/h1>/giu; | ||||||
|
||||||
/** | ||||||
* Checks if a frontmatter block contains a title matching the given pattern | ||||||
* @param {string} value The frontmatter content | ||||||
* @param {RegExp|null} pattern The pattern to match against | ||||||
* @returns {boolean} Whether a title was found | ||||||
*/ | ||||||
function frontmatterHasTitle(value, pattern) { | ||||||
if (!pattern) { | ||||||
return false; | ||||||
} | ||||||
const lines = value.split("\n"); | ||||||
for (const line of lines) { | ||||||
if (pattern.test(line)) { | ||||||
return true; | ||||||
} | ||||||
} | ||||||
return false; | ||||||
} | ||||||
|
||||||
//----------------------------------------------------------------------------- | ||||||
// Rule Definition | ||||||
//----------------------------------------------------------------------------- | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.