Advertisement
Krammy

Split note by horizontal line template

Dec 22nd, 2021
1,080
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <%*
  2. const content = tp.file.content;
  3. const sections = content.matchAll(/(?<=---|^)\s*([\S\s]+?)\s*(?=---|$)/g);
  4.  
  5. const tfile = this.app.workspace.getActiveFile();
  6. const my_folder = tfile.parent;
  7.  
  8. let number = 0;
  9.  
  10. function padToTwo(number) {
  11.     if (number <= 99)
  12.     {
  13.         number = ("0"+number).slice(-2);
  14.     }
  15.     return number;
  16. }
  17.  
  18. function get_title(section) {
  19.     title_match = section.match(/^#+ (.*)/m);
  20.    
  21.     if (title_match == null)
  22.         return "";
  23.    
  24.     return title_match[1];
  25. }
  26.  
  27. for (const section of sections)
  28. {
  29.     number += 1;
  30.    
  31.     note_content = section[1];
  32.     title = get_title(note_content);
  33.    
  34.     // remove disallowed characters
  35.     title = title.replace(/[*"\\/<>:|?]/g, "");
  36.    
  37.     if (title == "")
  38.         title = padToTwo(number);
  39.     else
  40.         // replace first header with header one
  41.         note_content = note_content.replace(/^#+ (.*)/m, "# $1");
  42.    
  43.     // replace two-line dash with three-line dash
  44.     note_content = note_content.replace(/(?<=\n|^)(--)(?=\n|$)/g, "---");
  45.    
  46.     await tp.file.create_new(
  47.         template = note_content,
  48.         filename = tp.user.get_note_ID() + " " + title,
  49.         open_new = false,
  50.         folder = my_folder
  51.     );
  52. }
  53. %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement