private void ExportSection(Node section)
        {
            if (!(section is Section sectionNote) || sectionNote.IsSectionGroup)
            {
                throw new InvalidOperationException("Cannot call ExportSection on section group with MdExport");
            }

            var pages = _oneNoteApp.GetPages(sectionNote);

            var resourceFolderPath = Path.Combine(section.GetNotebookPath(), "_resources");

            Directory.CreateDirectory(resourceFolderPath);

            int cmpt = 0;

            foreach (Page page in pages)
            {
                try
                {
                    Log.Information($"   Page {++cmpt}/{pages.Count} : {page.TitleWithPageLevelTabulation}");

                    Directory.CreateDirectory(page.GetPageFolderRelativePath());

                    var docxFilePath = page.GetPageFileRelativePath() + ".docx";

                    File.Delete(docxFilePath);
                    _oneNoteApp.Publish(page.OneNoteId, Path.GetFullPath(docxFilePath), PublishFormat.pfWord);

                    var mdFileContent = _convertServer.ConvertDocxToMd(page, docxFilePath, resourceFolderPath, section.GetLevel());
                    var mdFilePath    = page.GetPageFileRelativePath() + ".md";

                    try
                    {
                        mdFileContent = _convertServer.ExtractImagesToResourceFolder(page, mdFileContent, resourceFolderPath, mdFilePath, true, _appSettings.PostProcessingMdImgRef);
                    }
                    catch (Exception ex)
                    {
                        if (_appSettings.Debug)
                        {
                            Log.Warning($"Page '{page.GetPageFileRelativePath()}': {Localizer.GetString("ErrorImageExtract")}");
                        }
                        else
                        {
                            Log.Warning(ex, $"Page '{page.GetPageFileRelativePath()}'.");
                        }
                    }

                    mdFileContent = _convertServer.PostConvertion(page, mdFileContent, resourceFolderPath, mdFilePath, false);

                    File.WriteAllText(mdFilePath, mdFileContent);
                }
                catch (Exception e)
                {
                    Log.Error(Localizer.GetString("ErrorDuringPageProcessing"), page.TitleWithPageLevelTabulation, page.Id, e.Message);
                }
            }
        }
Example #2
0
        private void ExportSection(Node section)
        {
            var sectionMdFileContent = AddJoplinNodeMetadata(section, "");
            var notebookFolder       = section.GetNotebookPath();

            string onExportFolder;

            if (_appSettings.UserTempFolder)
            {
                onExportFolder = Path.GetTempPath();
            }
            else
            {
                onExportFolder = Path.Combine("tmp", notebookFolder);
                Directory.CreateDirectory(onExportFolder);
            }

            // Write Section Md File
            File.WriteAllText(Path.Combine(notebookFolder, $"{section.Id}.md"), sectionMdFileContent);

            if (section is Section sectionNode && !sectionNode.IsSectionGroup)
            {
                // For leaf section, export pages
                Log.Debug($"Start export pages of section {section.Title}");

                var pages = _oneNoteApp.GetPages(sectionNode);
                var resourceFolderPath = Path.Combine(notebookFolder, "resources");
                Directory.CreateDirectory(resourceFolderPath);

                int cmpt = 0;

                foreach (Page page in pages)
                {
                    Log.Information($"   Page {++cmpt}/{pages.Count} : {page.TitleWithPageLevelTabulation}");

                    var docxFilePath = Path.Combine(onExportFolder, page.Id + ".docx");

                    try
                    {
                        File.Delete(docxFilePath);

                        Log.Debug($"{page.OneNoteId}: start OneNote docx publish");
                        _oneNoteApp.Publish(page.OneNoteId, Path.GetFullPath(docxFilePath), PublishFormat.pfWord);
                        Log.Debug($"{page.OneNoteId}: success");

                        var mdFilePath = Path.Combine(notebookFolder, $"{page.Id}.md");

                        // Convert docx file into Md using PanDoc
                        var pageMdFileContent = _convertServer.ConvertDocxToMd(page, docxFilePath, resourceFolderPath, section.GetLevel());

                        try
                        {
                            // Copy images extracted from DocX to Export folder and add them in list of attachments of the note
                            pageMdFileContent = _convertServer.ExtractImagesToResourceFolder(page, pageMdFileContent, resourceFolderPath, mdFilePath, true, _appSettings.PostProcessingMdImgRef);
                        }
                        catch (Exception ex)
                        {
                            Log.Warning($"Page '{page.GetPageFileRelativePath()}': {Localizer.GetString("ErrorImageExtract")}");
                            Log.Debug(ex, ex.Message);
                        }

                        // Export all page attachments
                        pageMdFileContent = ExportPageAttachments(page, pageMdFileContent, notebookFolder, resourceFolderPath);

                        // Apply post processing to Page Md content
                        pageMdFileContent = _convertServer.PostConvertion(page, pageMdFileContent, resourceFolderPath, mdFilePath, true);

                        pageMdFileContent = AddJoplinNodeMetadata(page, pageMdFileContent);

                        // Create page md file
                        File.WriteAllText(mdFilePath, pageMdFileContent);


                        if (!_appSettings.Debug)
                        {
                            File.Delete(docxFilePath);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex, Localizer.GetString("ErrorDuringPageProcessing"), page.TitleWithPageLevelTabulation, page.Id, ex.Message);
                    }
                }
            }
        }