mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Merge pull request #8921 from overleaf/em-draft-mode
Change draft mode implementation GitOrigin-RevId: 3de81a3643cc024c410b7b49e77cd41c7fec8294
This commit is contained in:
parent
5f1abee345
commit
9199669c0f
2 changed files with 42 additions and 122 deletions
|
@ -1,61 +1,22 @@
|
|||
/* eslint-disable
|
||||
camelcase,
|
||||
no-useless-escape,
|
||||
*/
|
||||
// TODO: This file was created by bulk-decaffeinate.
|
||||
// Fix any style issues and re-enable lint.
|
||||
/*
|
||||
* decaffeinate suggestions:
|
||||
* DS102: Remove unnecessary code created because of implicit returns
|
||||
* DS207: Consider shorter variations of null checks
|
||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||
*/
|
||||
let DraftModeManager
|
||||
const fs = require('fs')
|
||||
const { promisify } = require('util')
|
||||
const fsPromises = require('fs/promises')
|
||||
const { callbackify } = require('util')
|
||||
const logger = require('@overleaf/logger')
|
||||
|
||||
module.exports = DraftModeManager = {
|
||||
injectDraftMode(filename, callback) {
|
||||
if (callback == null) {
|
||||
callback = function () {}
|
||||
}
|
||||
return fs.readFile(filename, 'utf8', function (error, content) {
|
||||
if (error != null) {
|
||||
return callback(error)
|
||||
}
|
||||
// avoid adding draft mode more than once
|
||||
if (
|
||||
(content != null
|
||||
? content.indexOf('\\documentclass[draft')
|
||||
: undefined) >= 0
|
||||
) {
|
||||
return callback()
|
||||
}
|
||||
const modified_content = DraftModeManager._injectDraftOption(content)
|
||||
logger.debug(
|
||||
{
|
||||
content: content.slice(0, 1024), // \documentclass is normally v near the top
|
||||
modified_content: modified_content.slice(0, 1024),
|
||||
filename,
|
||||
},
|
||||
'injected draft class'
|
||||
)
|
||||
return fs.writeFile(filename, modified_content, callback)
|
||||
})
|
||||
},
|
||||
|
||||
_injectDraftOption(content) {
|
||||
return (
|
||||
content
|
||||
// With existing options (must be first, otherwise both are applied)
|
||||
.replace(/\\documentclass\[/g, '\\documentclass[draft,')
|
||||
// Without existing options
|
||||
.replace(/\\documentclass\{/g, '\\documentclass[draft]{')
|
||||
)
|
||||
},
|
||||
async function injectDraftMode(filename) {
|
||||
const content = await fsPromises.readFile(filename, { encoding: 'utf8' })
|
||||
const modifiedContent = '\\PassOptionsToPackage{draft}{graphicx}' + content
|
||||
logger.debug(
|
||||
{
|
||||
content: content.slice(0, 1024), // \documentclass is normally v near the top
|
||||
modifiedContent: modifiedContent.slice(0, 1024),
|
||||
filename,
|
||||
},
|
||||
'injected draft class'
|
||||
)
|
||||
await fsPromises.writeFile(filename, modifiedContent, { encoding: 'utf8' })
|
||||
}
|
||||
|
||||
module.exports.promises = {
|
||||
injectDraftMode: promisify(DraftModeManager.injectDraftMode),
|
||||
module.exports = {
|
||||
injectDraftMode: callbackify(injectDraftMode),
|
||||
promises: { injectDraftMode },
|
||||
}
|
||||
|
|
|
@ -1,84 +1,43 @@
|
|||
/* eslint-disable
|
||||
no-return-assign,
|
||||
*/
|
||||
// TODO: This file was created by bulk-decaffeinate.
|
||||
// Fix any style issues and re-enable lint.
|
||||
/*
|
||||
* decaffeinate suggestions:
|
||||
* DS102: Remove unnecessary code created because of implicit returns
|
||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||
*/
|
||||
const Path = require('path')
|
||||
const fsPromises = require('fs/promises')
|
||||
const { expect } = require('chai')
|
||||
const mockFs = require('mock-fs')
|
||||
const SandboxedModule = require('sandboxed-module')
|
||||
const sinon = require('sinon')
|
||||
const modulePath = require('path').join(
|
||||
__dirname,
|
||||
'../../../app/js/DraftModeManager'
|
||||
)
|
||||
|
||||
const MODULE_PATH = Path.join(__dirname, '../../../app/js/DraftModeManager')
|
||||
|
||||
describe('DraftModeManager', function () {
|
||||
beforeEach(function () {
|
||||
return (this.DraftModeManager = SandboxedModule.require(modulePath, {
|
||||
this.DraftModeManager = SandboxedModule.require(MODULE_PATH, {
|
||||
requires: {
|
||||
fs: (this.fs = {}),
|
||||
'fs/promises': fsPromises,
|
||||
},
|
||||
}))
|
||||
})
|
||||
|
||||
describe('_injectDraftOption', function () {
|
||||
it('should add draft option into documentclass with existing options', function () {
|
||||
return this.DraftModeManager._injectDraftOption(`\
|
||||
\\documentclass[a4paper,foo=bar]{article}\
|
||||
`).should.equal(`\
|
||||
\\documentclass[draft,a4paper,foo=bar]{article}\
|
||||
`)
|
||||
})
|
||||
|
||||
return it('should add draft option into documentclass with no options', function () {
|
||||
return this.DraftModeManager._injectDraftOption(`\
|
||||
\\documentclass{article}\
|
||||
`).should.equal(`\
|
||||
\\documentclass[draft]{article}\
|
||||
`)
|
||||
})
|
||||
})
|
||||
|
||||
return describe('injectDraftMode', function () {
|
||||
beforeEach(function () {
|
||||
this.filename = '/mock/filename.tex'
|
||||
this.callback = sinon.stub()
|
||||
const content = `\
|
||||
this.filename = '/mock/filename.tex'
|
||||
this.contents = `\
|
||||
\\documentclass{article}
|
||||
\\begin{document}
|
||||
Hello world
|
||||
\\end{document}\
|
||||
`
|
||||
this.fs.readFile = sinon.stub().callsArgWith(2, null, content)
|
||||
this.fs.writeFile = sinon.stub().callsArg(2)
|
||||
return this.DraftModeManager.injectDraftMode(this.filename, this.callback)
|
||||
mockFs({
|
||||
[this.filename]: this.contents,
|
||||
})
|
||||
})
|
||||
|
||||
it('should read the file', function () {
|
||||
return this.fs.readFile
|
||||
.calledWith(this.filename, 'utf8')
|
||||
.should.equal(true)
|
||||
})
|
||||
afterEach(function () {
|
||||
mockFs.restore()
|
||||
})
|
||||
|
||||
it('should write the modified file', function () {
|
||||
return this.fs.writeFile
|
||||
.calledWith(
|
||||
this.filename,
|
||||
`\
|
||||
\\documentclass[draft]{article}
|
||||
\\begin{document}
|
||||
Hello world
|
||||
\\end{document}\
|
||||
`
|
||||
)
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
return it('should call the callback', function () {
|
||||
return this.callback.called.should.equal(true)
|
||||
describe('injectDraftMode', function () {
|
||||
it('prepends a special command to the beginning of the file', async function () {
|
||||
await this.DraftModeManager.promises.injectDraftMode(this.filename)
|
||||
const contents = await fsPromises.readFile(this.filename, {
|
||||
encoding: 'utf8',
|
||||
})
|
||||
expect(contents).to.equal(
|
||||
'\\PassOptionsToPackage{draft}{graphicx}' + this.contents
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue