extension.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. const path = require("path");
  2. const mimeTypes = require("mime-types");
  3. function getFileName(note, childTargetPath, safeTitle) {
  4. let existingExtension = path.extname(safeTitle).toLowerCase();
  5. let newExtension;
  6. if (note.type === 'text') {
  7. newExtension = 'html';
  8. } else if (note.mime === 'application/x-javascript' || note.mime === 'text/javascript') {
  9. newExtension = 'js';
  10. } else if (existingExtension.length > 0) { // if the page already has an extension, then we'll just keep it
  11. newExtension = null;
  12. } else {
  13. if (note.mime?.toLowerCase()?.trim() === "image/jpg") { // image/jpg is invalid but pretty common
  14. newExtension = 'jpg';
  15. } else {
  16. newExtension = mimeTypes.extension(note.mime) || "dat";
  17. }
  18. }
  19. let fileNameWithPath = childTargetPath;
  20. // if the note is already named with extension (e.g. "jquery"), then it's silly to append exact same extension again
  21. if (newExtension && existingExtension !== "." + newExtension.toLowerCase()) {
  22. fileNameWithPath += "." + newExtension;
  23. }
  24. return fileNameWithPath;
  25. }
  26. module.exports = {
  27. getFileName
  28. };