From 575221c90e4ed0a73e1e9afb523cc4dc580f71a0 Mon Sep 17 00:00:00 2001 From: Benjamin Leonard Date: Sat, 24 Sep 2022 18:00:03 +0100 Subject: [PATCH] Add `getSectionNumeral()` function (#1659) Co-authored-by: Guillaume Grossetie --- packages/core/spec/share/asciidoctor-spec.cjs | 5 +++++ packages/core/src/asciidoctor-core-api.js | 11 +++++++++++ packages/core/types/index.d.ts | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/packages/core/spec/share/asciidoctor-spec.cjs b/packages/core/spec/share/asciidoctor-spec.cjs index 870ec5bd1..ec5ebf110 100644 --- a/packages/core/spec/share/asciidoctor-spec.cjs +++ b/packages/core/spec/share/asciidoctor-spec.cjs @@ -594,6 +594,7 @@ This is another paragraph. expect(firstSection.title).to.equal('First section') expect(firstSection.getSectionName()).to.equal('section') expect(firstSection.isNumbered()).to.equal(false) + expect(firstSection.getSectionNumeral()).to.equal('.') expect(firstSection.isSpecial()).to.equal(false) expect(firstSection.getCaption()).to.be.undefined() const secondSection = doc.getSections()[1] @@ -603,6 +604,8 @@ This is another paragraph. expect(secondSection.title).to.equal('Second section') expect(secondSection.getSectionName()).to.equal('section') expect(secondSection.isNumbered()).to.equal(true) + expect(secondSection.getSectionNumeral()).to.equal('1.') + expect(secondSection.getSectionNumber()).to.equal('1.') expect(secondSection.isSpecial()).to.equal(false) expect(secondSection.getCaption()).to.be.undefined() const abstractSection = doc.getSections()[2] @@ -612,6 +615,7 @@ This is another paragraph. expect(abstractSection.title).to.equal('Abstract section') expect(abstractSection.getSectionName()).to.equal('abstract') expect(abstractSection.isNumbered()).to.equal(false) + expect(firstSection.getSectionNumeral()).to.equal('.') expect(abstractSection.isSpecial()).to.equal(true) expect(abstractSection.getCaption()).to.be.undefined() const appendixSection = doc.getSections()[3] @@ -620,6 +624,7 @@ This is another paragraph. expect(appendixSection.getTitle()).to.equal('Copyright and License') expect(appendixSection.title).to.equal('Copyright and License') expect(appendixSection.getSectionName()).to.equal('appendix') + expect(appendixSection.getSectionNumeral()).to.equal('A.') expect(appendixSection.isNumbered()).to.equal(true) expect(appendixSection.isSpecial()).to.equal(true) expect(appendixSection.getCaption()).to.equal('Appx A: ') diff --git a/packages/core/src/asciidoctor-core-api.js b/packages/core/src/asciidoctor-core-api.js index e18c6262b..ae8e486f2 100644 --- a/packages/core/src/asciidoctor-core-api.js +++ b/packages/core/src/asciidoctor-core-api.js @@ -744,6 +744,17 @@ Section.prototype.setSectionName = function (value) { this.sectname = value } +/** + * Get the section numeral of this section. + * @returns {string} + * @memberof Section + */ +Section.prototype.getSectionNumeral = function () { + return this.$sectnum() +} + +Section.prototype.getSectionNumber = Section.prototype.getSectionNumeral + /** * Get the flag to indicate whether this is a special section or a child of one. * @returns {boolean} diff --git a/packages/core/types/index.d.ts b/packages/core/types/index.d.ts index 49267ec33..d3f841e01 100644 --- a/packages/core/types/index.d.ts +++ b/packages/core/types/index.d.ts @@ -2247,6 +2247,14 @@ export namespace Asciidoctor { */ setSectionName(value: string): void; + /** + * Get the section number of this section. + */ + getSectionNumeral(): string; + + // alias + getSectionNumber(): string; + /** * Get the flag to indicate whether this is a special section or a child of one. */