Skip to content

Instantly share code, notes, and snippets.

@jmatsushita
Forked from pmbauer/pseudo_focus.css
Last active December 3, 2023 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmatsushita/ff53393057982216260404abebc003bf to your computer and use it in GitHub Desktop.
Save jmatsushita/ff53393057982216260404abebc003bf to your computer and use it in GitHub Desktop.
Obsidian CSS snippet to mimic source mode in live preview mode
/***
* Common tweaks
***/
/* Using the Github Theme with a few tweaks */
.theme-dark {
--background-primary: #1f1f1f;
}
/* display headings with normal font size */
.cm-s-obsidian .HyperMD-header,
.cm-s-obsidian span.cm-formatting-link {
font-size: var(--font-text-size);
color: #569cd6;
}
.cm-s-obsidian .cm-line.HyperMD-header {
padding-top: var(--size-2-1);
}
/* don't indent code blocks */
.markdown-source-view.mod-cm6 .cm-line.HyperMD-codeblock {
padding-left: 0;
}
/* display hashtags with normal font size */
.cm-hashtag {
font-size: var(--font-text-size);
}
/* display headings as in source mode */
/* adapted from https://gist.github.com/pmbauer/7395acded3bf74d41fe70a2e70d7952a#file-pseudo_focus-css-L11-L40 */
.is-live-preview {
& .cm-header::before {
position: relative;
bottom: 0px;
}
& .HyperMD-header-1.cm-line:not(.cm-active)::before {
content: "# ";
}
& .HyperMD-header-2.cm-line:not(.cm-active)::before {
content: "## ";
}
& .HyperMD-header-3.cm-line:not(.cm-active)::before {
content: "### ";
}
& .HyperMD-header-4.cm-line:not(.cm-active)::before {
content: "#### ";
}
& .HyperMD-header-5.cm-line:not(.cm-active)::before {
content: "##### ";
}
& .HyperMD-header-6.cm-line:not(.cm-active)::before {
content: "###### ";
}
/* hide the automatically inserted heading in live preview */
/* move it out of the viewport instead of display:none */
& .cm-line > .cm-formatting-header {
position: absolute;
left: -999px;
}
/* only first header element gets the prefix */
& .cm-header:not(.cm-formatting-header) ~ .cm-header::before {
display: none;
}
/* display urls in same color as text but with underline */
& .cm-url {
color: var(--text-normal);
text-decoration-line: var(--link-external-decoration-hover);
}
& span.cm-url {
color: var(--text-normal);
text-decoration-line: var(--link-external-decoration-hover);
}
/* display code blocks with same font size and background color as source mode */
& span.cm-inline-code {
font-size: var(--font-text-size);
background-color: var(--background-primary);
}
& div.HyperMD-codeblock-bg {
background-color: var(--background-primary);
}
/* don't hide backquotes */
& span.cm-inline-code:not(.cm-formatting, :has(+ .cm-formatting))::before {
content: "`";
}
& span.cm-inline-code:not(.cm-formatting, :has(+ .cm-formatting))::after {
content: "`";
padding-left: 3px;
}
/* adjust margins to prevent relayout */
.cm-inline-code:not(.cm-formatting) + .cm-inline-code.cm-formatting {
margin-right: 3px;
padding-left: 0px;
}
.cm-inline-code.cm-formatting:not(:has(.cm-formatting)) {
margin-left: 3px;
}
}
/***
* Only for desktop app since there are problems
* with the `.has()` selector interacting with the live editing
***/
body:not(.is-mobile) {
.cm-s-obsidian {
/* display urls in same color as text but with underline */
& .cm-url {
color: var(--text-normal);
text-decoration-line: var(--link-external-decoration-hover);
}
& span.cm-url {
color: var(--text-normal);
text-decoration-line: var(--link-external-decoration-hover);
}
/* display code blocks with same font size and background color as source mode */
& span.cm-inline-code {
font-size: var(--font-text-size);
background-color: var(--background-primary);
}
& div.HyperMD-codeblock-bg {
background-color: var(--background-primary);
}
& .HyperMD-codeblock {
font-size: var(--font-text-size);
}
& .cm-active.HyperMD-codeblock-begin::before {
content: "";
}
& .cm-active.HyperMD-codeblock-end::after {
content: "";
}
/* Weeeee */
& .HyperMD-codeblock-begin:not(:has(> .cm-formatting-code-block))::before {
content: "```";
}
& .HyperMD-codeblock-end:not(:has(> .cm-formatting-code-block))::after {
content: "```";
}
}
/* display copy-paste flair close to code block language identifier */
.markdown-source-view.mod-cm6 .code-block-flair {
font-family: var(--font-text-family);
font-size: var(--font-text-size);
right: unset;
padding: 0;
top: 0px;
}
}
@jmatsushita
Copy link
Author

The idea is to mimic my VScode settings (or Obsidian's Source mode) in Obsidian's Live Preview mode. As I prefer the interface to not jump over to much when I enter or leave smart previewing spans.

Screenshot 2023-12-02 at 19 13 27 Screenshot 2023-12-02 at 19 15 34

I didn't find that I needed bold and emphasis to be treated the same way, but I did go the extra mile to make code blocks behave nicely (a matter of taste of course!):

obsidian_codeblocks.mov

@jmatsushita
Copy link
Author

jmatsushita commented Dec 2, 2023

Oopsie, creating new headings is broken 😓

UPDATE: I tried to remove the flashing header when entering and leaving header editing from the original gist, but I didn't test header creation.
UPDATE 2: It also doesn't work properly on the mobile app (after enabling sync of theme snippets).
UPDATE 3: Both are fixed now.

@jmatsushita
Copy link
Author

jmatsushita commented Dec 2, 2023

Fixed it by moving the cm-formatting-header out of the view port instead of using display: none;

  /* hide the automatically inserted heading in live preview */
  & .cm-line > .cm-formatting-header {
    position: absolute;
    left: -999px;
  }

My attention to detail is a bit unhappy with the fact that the caret is hidden just after entering # then [space] and only reappears when entering the next character... but I'll live.

@jmatsushita
Copy link
Author

Seems that maybe the .has() is not too happy on mobile, maybe because of the interaction with the live editing of code blocks (which seems a bit laggy on ios. So for now I've only enabled the code block overrides for the desktop app.

@jmatsushita
Copy link
Author

jmatsushita commented Dec 3, 2023

Added small tweaks for inline code blocks to make them not relayout when switching in and out of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment