NotebookLM Codeblock Wrap & Section Breaks

Force line wrap + add section breaks for codeblocks in NotebookLM Studio/Notes panel.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         NotebookLM Codeblock Wrap & Section Breaks
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Force line wrap + add section breaks for codeblocks in NotebookLM Studio/Notes panel.
// @match        https://notebooklm.google.com/*
// @grant        GM_addStyle
// ==/UserScript==

(function () {
    'use strict';

    // 1. CSS: Maximum specificity for all code/pre permutations in Studio and Chat (covers all <pre.ng-star-inserted> > code)
    GM_addStyle(`
      pre.ng-star-inserted,
      pre.ng-star-inserted > code {
        white-space: pre-wrap !important;
        word-break: break-word !important;
        overflow-wrap: anywhere !important;
      }
    `);

    // 2. JS backup: forcibly set the style for both <pre> and <code> elements if needed.
    function forceWrapStudioCodeBlocks() {
        document.querySelectorAll('pre.ng-star-inserted, pre.ng-star-inserted > code').forEach(block => {
            block.style.whiteSpace = 'pre-wrap';
            block.style.wordBreak = 'break-word';
            block.style.overflowWrap = 'anywhere';
        });
    }

    // 3. Section breaks for code: extend as before, matches chat and studio panels
    function addSectionBreaksToStudioCodeBlocks() {
        document.querySelectorAll('pre.ng-star-inserted > code').forEach(codeBlock => {
            if (codeBlock.dataset.sectionsFormatted) return;
            const lines = codeBlock.textContent.split('\n');
            const sectionRegex = /^\[[^\]]+:/;
            let newHTML = lines.map(line => {
                if (sectionRegex.test(line.trim())) {
                    return `${line}<br><br>`;
                }
                return line;
            }).join('\n');
            codeBlock.innerHTML = newHTML.replace(/\n/g, '<br>');
            codeBlock.dataset.sectionsFormatted = "true";
        });
    }

    // 4. Initialize and set up observer for dynamic content (Studio/Note or Chat, works everywhere)
    forceWrapStudioCodeBlocks();
    addSectionBreaksToStudioCodeBlocks();
    const observer = new MutationObserver(() => {
        forceWrapStudioCodeBlocks();
        addSectionBreaksToStudioCodeBlocks();
    });
    observer.observe(document.body, { childList: true, subtree: true });
})();