Skip to content

Commit

Permalink
[zmarkdown] Fix introduction/conclusion shifts (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
StaloneLab authored Feb 27, 2021
1 parent 00b63dd commit 7cdfa0e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
25 changes: 25 additions & 0 deletions packages/zmarkdown/__tests__/__snapshots__/server.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,31 @@ exports[`Texfile endpoint escapes title and author 1`] = `
\\\\levelOneTitle{foo}
\\\\end{document}"
`;
exports[`Texfile endpoint shifts titles and only titles 1`] = `
"\\\\documentclass[contentType]{zmdocument}
\\\\usepackage{blindtext}
\\\\title{The Title}
\\\\author{FØØ, Bär}
\\\\licence[/tmp/l/by-nc-sa.svg]{CC-BY-NC-SA}{https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode}
\\\\smileysPath{/tmp/s}
\\\\makeglossaries
\\\\begin{document}
\\\\maketitle
\\\\tableofcontents
\\\\levelThreeTitle{myTitle}
\\\\begin{LevelOneIntroduction}
myIntro
\\\\end{LevelOneIntroduction}
\\\\end{document}"
`;
Expand Down
18 changes: 18 additions & 0 deletions packages/zmarkdown/__tests__/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,24 @@ describe('Texfile endpoint', () => {
expect(content).toContain('LevelThreeIntroduction')
expect(content).toContain('LevelThreeConclusion')
})

it('shifts titles and only titles', async () => {
const opts = clone(texfileOpts)
const manifest = {
introduction: 'myIntro',
title: 'myTitle',
}

opts.heading_shift = 2

const response = await a.post(texfile, {md: manifest, opts})
expect(response.status).toBe(200)

const [content] = response.data
expect(content).toMatchSnapshot()
expect(content).toContain('LevelOneIntroduction')
expect(content).toContain('levelThreeTitle')
})
})


Expand Down
2 changes: 1 addition & 1 deletion packages/zmarkdown/server/factories/config-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = opts => {
if (['introduction', 'conclusion'].includes(opts.extract_type)) {
mdastConfig.postProcessors.wrapIntroCcl = {
type: opts.extract_type,
level: opts.heading_shift || 0,
level: opts.ic_shift || 0,
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion packages/zmarkdown/server/factories/controller-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ module.exports = (givenProc, template) => (req, res) => {
// Get a collection of Promises to execute
if (manifestRender) {
extractPromises = manifest
.gatherExtracts(rawContent, baseShift)
.gatherExtracts(rawContent)
.map(extract => {
// Manifest rendering requires forging a new processor
// to handle title depths
const {text, options: localOptions} = extract
if (localOptions.depth) {
localOptions.heading_shift = baseShift + localOptions.depth
localOptions.ic_shift = localOptions.depth
}

const mergedOptions = Object.assign({}, options, localOptions)
const processor = processorFactory(givenProc, mergedOptions, true)

Expand Down
10 changes: 5 additions & 5 deletions packages/zmarkdown/server/utils/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const listConcatUnique = (a, b) => {
const executeOnExtracts = (children, execFunction, depth = 0) => {
function changeIfReturn (obj, extractType, d) {
const r = execFunction(obj[extractType], {
extract_type: extractType,
heading_shift: d,
extract_type: extractType,
depth: d,
})
if (typeof r === 'string') obj[extractType] = r
}
Expand Down Expand Up @@ -50,15 +50,15 @@ const metadataPropertiesRules = {
ping: listConcatUnique,
}

manifestUtils.gatherExtracts = (manifest, baseShift) => {
manifestUtils.gatherExtracts = manifest => {
const rawExtracts = []

const appendToExtracts = (text, options = {}) => {
rawExtracts.push({text, options})
}

// Start recursion by top-level element
executeOnExtracts([manifest], appendToExtracts, baseShift)
executeOnExtracts([manifest], appendToExtracts)

return rawExtracts
}
Expand Down Expand Up @@ -115,7 +115,7 @@ manifestUtils.dispatch = (parsedExtracts, originalManifest) => {

// Replace each extract where it belongs
let i = 0
executeOnExtracts([finalManifest], () => parsedExtracts[i++].contents, 0)
executeOnExtracts([finalManifest], () => parsedExtracts[i++].contents)

assembledExtracts.contents = finalManifest
return assembledExtracts
Expand Down

0 comments on commit 7cdfa0e

Please sign in to comment.