Skip to content

Commit

Permalink
Add data-line attrs support for heading and paragraph (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
sofiushko authored Nov 17, 2023
1 parent 9c70010 commit bee79c1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
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

0 comments on commit bee79c1

Please sign in to comment.