DraftModeManager.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* eslint-disable
  2. camelcase,
  3. no-useless-escape,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. let DraftModeManager
  14. const fs = require('fs')
  15. const logger = require('@overleaf/logger')
  16. module.exports = DraftModeManager = {
  17. injectDraftMode(filename, callback) {
  18. if (callback == null) {
  19. callback = function () {}
  20. }
  21. return fs.readFile(filename, 'utf8', function (error, content) {
  22. if (error != null) {
  23. return callback(error)
  24. }
  25. // avoid adding draft mode more than once
  26. if (
  27. (content != null
  28. ? content.indexOf('\\documentclass[draft')
  29. : undefined) >= 0
  30. ) {
  31. return callback()
  32. }
  33. const modified_content = DraftModeManager._injectDraftOption(content)
  34. logger.debug(
  35. {
  36. content: content.slice(0, 1024), // \documentclass is normally v near the top
  37. modified_content: modified_content.slice(0, 1024),
  38. filename,
  39. },
  40. 'injected draft class'
  41. )
  42. return fs.writeFile(filename, modified_content, callback)
  43. })
  44. },
  45. _injectDraftOption(content) {
  46. return (
  47. content
  48. // With existing options (must be first, otherwise both are applied)
  49. .replace(/\\documentclass\[/g, '\\documentclass[draft,')
  50. // Without existing options
  51. .replace(/\\documentclass\{/g, '\\documentclass[draft]{')
  52. )
  53. },
  54. }