Skip to content

Commit

Permalink
add slide separator in case no separator is provided and heading indi…
Browse files Browse the repository at this point in the history
…cates the new slide
  • Loading branch information
PrajwolAmatya committed Jul 12, 2024
1 parent c4005de commit 3c1659e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
2 changes: 1 addition & 1 deletion plugin/awesoMD/awesoMD.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion plugin/awesoMD/awesoMD.js

Large diffs are not rendered by default.

66 changes: 53 additions & 13 deletions plugin/awesoMD/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ const Plugin = () => {
options.separator = options.separator || markdownConfig?.separator || DEFAULT_SLIDE_SEPARATOR;
options.verticalSeparator = options.verticalSeparator || markdownConfig?.verticalSeparator || DEFAULT_VERTICAL_SEPARATOR;
options.notesSeparator = options.notesSeparator || markdownConfig?.notesSeparator || DEFAULT_NOTES_SEPARATOR;
options.separateByHeading = options.separateByHeading || markdownConfig?.separateByHeading || false;
options.attributes = options.attributes || '';

return options;
Expand Down Expand Up @@ -146,7 +147,13 @@ const Plugin = () => {
*/
function slidify( markdown, options ) {

options = getSlidifyOptions( options );
options = getSlidifyOptions( options )

// add slide separator in the case heading indicates the new slide
if (options.separateByHeading) {
options['slideSeparator'] = '---'
markdown = addSlideSeparator(markdown, options)
}

const separatorRegex = new RegExp( options.separator + ( options.verticalSeparator ? '|' + options.verticalSeparator : '' ), 'mg' ),
horizontalSeparatorRegex = new RegExp( options.separator );
Expand Down Expand Up @@ -243,6 +250,7 @@ const Plugin = () => {
separator: section.getAttribute( 'data-separator' ),
verticalSeparator: section.getAttribute( 'data-separator-vertical' ),
notesSeparator: section.getAttribute( 'data-separator-notes' ),
separateByHeading: section.hasAttribute('data-separator-by-heading'),
attributes: getForwardedAttributes( section )
});
},
Expand All @@ -265,6 +273,7 @@ const Plugin = () => {
separator: section.getAttribute( 'data-separator' ),
verticalSeparator: section.getAttribute( 'data-separator-vertical' ),
notesSeparator: section.getAttribute( 'data-separator-notes' ),
separateByHeading: section.hasAttribute('data-separator-by-heading'),
attributes: getForwardedAttributes( section )
});

Expand Down Expand Up @@ -458,6 +467,33 @@ const Plugin = () => {
return [content, options];
}

/**
* Add slide separator in case where the heading indicates the start of new slide
*
* Returns the updated markdown file with added slide separator above every slide headings
*/
function addSlideSeparator(markdown, options) {
const lines = markdown.split('\n')
const result = []
let firstHeadingProcessingDone = false

lines.forEach((line, index) => {
if (line.match(/^#{1,6}\s+/)) {
if (!firstHeadingProcessingDone) {
firstHeadingProcessingDone = true
} else {
const previousLine = lines[index - 1] || ''
if (previousLine !== options.slideSeparator) {
result.push(options.slideSeparator)
}
}
}
result.push(line)
})
markdown = result.join('\n')
return markdown
}

/**
* Separates the inline metadata and content for slide having inline metadata in yaml block as
*
Expand Down Expand Up @@ -525,18 +561,22 @@ const Plugin = () => {
const newMetadata = headingWithMetadataRegex.test(markdown)
yamlRegex.lastIndex = 0

switch (true) {
case newMetadata:
[markdown, options] = extractInlineMetadata(markdown, options)
break
case yamlMetadata:
[markdown, options] = extractYAMLMetadata(markdown, options)
break
default:
if (options.metadata) {
options.attributes = 'class=' + (options.metadata.slide || '')
}
break
if (options.separateByHeading) {
[markdown, options] = extractInlineMetadata(markdown, options)
} else {
switch (true) {
case newMetadata:
[markdown, options] = extractInlineMetadata(markdown, options)
break
case yamlMetadata:
[markdown, options] = extractYAMLMetadata(markdown, options)
break
default:
if (options.metadata) {
options.attributes = 'class=' + (options.metadata.slide || '')
}
break
}
}

return [markdown, options]
Expand Down

0 comments on commit 3c1659e

Please sign in to comment.