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

feat(YfmHeading): support folding attribute #285

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/extensions/yfm/YfmHeading/YfmHeadingSpecs/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export const YfmHeadingAttr = {
Level: headingLevelAttr,
Id: 'id',
DataLine: 'data-line',
Folding: 'folding',
} as const;
9 changes: 8 additions & 1 deletion src/extensions/yfm/YfmHeading/YfmHeadingSpecs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const YfmHeadingSpecs: ExtensionAuto<YfmHeadingSpecsOptions> = (builder,
[YfmHeadingAttr.Id]: {default: ''},
[YfmHeadingAttr.Level]: {default: 1},
[YfmHeadingAttr.DataLine]: {default: null},
[YfmHeadingAttr.Folding]: {default: false},
},
content: '(text | inline)*',
group: 'block',
Expand All @@ -40,11 +41,13 @@ export const YfmHeadingSpecs: ExtensionAuto<YfmHeadingSpecsOptions> = (builder,
toDOM(node) {
const id = node.attrs[YfmHeadingAttr.Id];
const lineNumber = node.attrs[YfmHeadingAttr.DataLine];
const folding = node.attrs[YfmHeadingAttr.Folding];
return [
'h' + node.attrs[YfmHeadingAttr.Level],
{
id: id || null,
[YfmHeadingAttr.DataLine]: lineNumber,
[`data-${YfmHeadingAttr.Folding}`]: folding ? '' : null,
},
0,
// [
Expand Down Expand Up @@ -84,13 +87,17 @@ export const YfmHeadingSpecs: ExtensionAuto<YfmHeadingSpecsOptions> = (builder,
// attrs have id only if it explicitly specified manually
return {
[YfmHeadingAttr.Level]: Number(token.tag.slice(1)),
[YfmHeadingAttr.Folding]: token.meta?.folding,
...attrs,
};
},
},
},
toMd: (state, node) => {
state.write(state.repeat('#', node.attrs[YfmHeadingAttr.Level]) + ' ');
const folding = node.attrs[YfmHeadingAttr.Folding];
const level = node.attrs[YfmHeadingAttr.Level];

state.write(state.repeat('#', level) + (folding ? '+' : '') + ' ');
state.renderInline(node);

const anchor = node.attrs[YfmHeadingAttr.Id];
Expand Down
3 changes: 2 additions & 1 deletion src/extensions/yfm/YfmHeading/YfmHeadingSpecs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export const getNodeAttrs =
(level: HeadingLevel): TagParseRule['getAttrs'] =>
(node) => ({
[YfmHeadingAttr.Level]: level,
[YfmHeadingAttr.Id]: (node as Element).getAttribute('id') || '',
[YfmHeadingAttr.Id]: node.getAttribute('id') || '',
[YfmHeadingAttr.Folding]: node.hasAttribute(`data-${YfmHeadingAttr.Folding}`),
});

// export const slugify = (str: string) =>
Expand Down
16 changes: 10 additions & 6 deletions src/extensions/yfm/YfmHeading/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ export {resetHeading} from '../../markdown/Heading/commands';
export const toHeading =
(level: HeadingLevel): Command =>
(state, dispatch, view) => {
const attrs: Record<string, any> = {};

const parentHeading = findParentNodeOfType(hType(state.schema))(state.selection);
if (parentHeading && parentHeading.node.attrs[headingLevelAttr] === level) {
return toParagraph(state, dispatch, view);
if (parentHeading) {
if (parentHeading.node.attrs[headingLevelAttr] === level) {
return toParagraph(state, dispatch, view);
}

Object.assign(attrs, parentHeading.node.attrs);
}

// const text = state.selection.$head.parent.textContent;
const attrs = {
// [YfmHeadingAttr.Id]: slugify(text),
[YfmHeadingAttr.Level]: level,
};
// attrs[YfmHeadingAttr.Id] = slugify(text);
attrs[YfmHeadingAttr.Level] = level;

return setBlockType(hType(state.schema), attrs)(state, dispatch);
};
Loading