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

Add data-line attrs support for heading and paragraph #158

Merged
merged 1 commit into from
Nov 17, 2023
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
13 changes: 11 additions & 2 deletions src/extensions/base/BaseSchema/BaseSchemaSpecs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export enum BaseNode {
Paragraph = 'paragraph',
}

const paragraphLineNumberAttr = 'data-line';

export const pType = nodeTypeFactory(BaseNode.Paragraph);

export type BaseSchemaSpecsOptions = {
Expand Down Expand Up @@ -38,11 +40,18 @@ export const BaseSchemaSpecs: ExtensionAuto<BaseSchemaSpecsOptions> = (builder,
}))
.addNode(BaseNode.Paragraph, () => ({
spec: {
attrs: {[paragraphLineNumberAttr]: {default: null}},
content: 'inline*',
group: 'block',
parseDOM: [{tag: 'p'}],
toDOM() {
return ['p', 0];
toDOM(node) {
const lineNumber = node.attrs[paragraphLineNumberAttr];

return [
'p',
lineNumber === null ? {} : {[paragraphLineNumberAttr]: lineNumber},
0,
];
},
placeholder: opts.paragraphPlaceholder
? {
Expand Down
11 changes: 9 additions & 2 deletions src/extensions/markdown/Heading/HeadingSpecs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {nodeTypeFactory} from '../../../../utils/schema';

export const headingNodeName = 'heading';
export const headingLevelAttr = 'level';
export const headingLineNumberAttr = 'data-line';
export const headingType = nodeTypeFactory(headingNodeName);

const DEFAULT_PLACEHOLDER = (node: Node) => 'Heading ' + node.attrs[headingLevelAttr];
Expand All @@ -18,7 +19,7 @@ export type HeadingSpecsOptions = {
export const HeadingSpecs: ExtensionAuto<HeadingSpecsOptions> = (builder, opts) => {
builder.addNode(headingNodeName, () => ({
spec: {
attrs: {[headingLevelAttr]: {default: 1}},
attrs: {[headingLevelAttr]: {default: 1}, [headingLineNumberAttr]: {default: null}},
content: '(text | inline)*',
group: 'block',
defining: true,
Expand All @@ -31,7 +32,13 @@ export const HeadingSpecs: ExtensionAuto<HeadingSpecsOptions> = (builder, opts)
{tag: 'h6', attrs: {[headingLevelAttr]: 6}},
],
toDOM(node) {
return ['h' + node.attrs[headingLevelAttr], 0];
const lineNumber = node.attrs[headingLineNumberAttr];

return [
'h' + node.attrs[headingLevelAttr],
lineNumber === null ? {} : {[headingLineNumberAttr]: lineNumber},
0,
];
},
placeholder: {
content:
Expand Down
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 @@ -6,4 +6,5 @@ export {headingLevelAttr, headingNodeName} from '../../../markdown/Heading/Headi
export const YfmHeadingAttr = {
Level: headingLevelAttr,
Id: 'id',
DataLine: 'data-line',
} as const;
7 changes: 6 additions & 1 deletion src/extensions/yfm/YfmHeading/YfmHeadingSpecs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const YfmHeadingSpecs: ExtensionAuto<YfmHeadingSpecsOptions> = (builder,
attrs: {
[YfmHeadingAttr.Id]: {default: ''},
[YfmHeadingAttr.Level]: {default: 1},
[YfmHeadingAttr.DataLine]: {default: null},
},
content: '(text | inline)*',
group: 'block',
Expand All @@ -36,9 +37,13 @@ export const YfmHeadingSpecs: ExtensionAuto<YfmHeadingSpecsOptions> = (builder,
],
toDOM(node) {
const id = node.attrs[YfmHeadingAttr.Id];
const lineNumber = node.attrs[YfmHeadingAttr.DataLine];
return [
'h' + node.attrs[YfmHeadingAttr.Level],
id ? {id} : {},
{
id: id || null,
[YfmHeadingAttr.DataLine]: lineNumber,
},
0,
// [
// 'a',
Expand Down
Loading