Skip to content

Commit

Permalink
Render and hide metadata
Browse files Browse the repository at this point in the history
Depending on the user setting.

This fixes issue #37
#37
  • Loading branch information
c3er committed Jul 24, 2023
1 parent c22c38c commit c9f108b
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 2 deletions.
10 changes: 10 additions & 0 deletions app/lib/documentRendering/documentRenderingMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ENABLE_TYPOGRAPHY_MENU_ID = "enable-typographic-replacements"
const ENABLE_EMOJIS_MENU_ID = "enable-emojis"
const RENDER_FILE_AS_MD_MENU_ID = "render-file-as-markdown"
const RENDER_FILE_TYPE_AS_MD_MENU_ID = "render-file-type-as-markdown"
const HIDE_METADATA_MENU_ID = "hide-metadata"

const UPDATE_FILE_SPECIFICA_NAV_ID = "update-file-specific-document-rendering"

Expand Down Expand Up @@ -41,6 +42,7 @@ function notifyOptionChanges(filePath) {
typographyEnabled: _applicationSettings.typographyEnabled,
emojisEnabled: _applicationSettings.emojisEnabled,
renderAsMarkdown: documentSettings.renderAsMarkdown || isMarkdownFileType(filePath),
hideMetadata: _applicationSettings.hideMetadata,
})
}

Expand Down Expand Up @@ -68,6 +70,8 @@ exports.RENDER_FILE_AS_MD_MENU_ID = RENDER_FILE_AS_MD_MENU_ID

exports.RENDER_FILE_TYPE_AS_MD_MENU_ID = RENDER_FILE_TYPE_AS_MD_MENU_ID

exports.HIDE_METADATA_MENU_ID = HIDE_METADATA_MENU_ID

exports.init = (mainMenu, applicationSettings, filePath) => {
_mainMenu = mainMenu
_applicationSettings = applicationSettings
Expand Down Expand Up @@ -130,3 +134,9 @@ exports.switchRenderFileTypeAsMarkdown = filePath =>
menu.getChecked(_mainMenu, RENDER_FILE_TYPE_AS_MD_MENU_ID),
),
)

exports.hideMetadata = () =>
changeOption(
() =>
(_applicationSettings.hideMetadata = menu.getChecked(_mainMenu, HIDE_METADATA_MENU_ID)),
)
6 changes: 5 additions & 1 deletion app/lib/documentRendering/documentRenderingRenderer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const hljs = require("highlight.js")

const metadata = require("../renderer/metadata")
const toc = require("../toc/tocRenderer")

let _markdown

let _shallRenderAsMarkdown = false
let _shallHideMetadata = false

function generateCodeText(text, options = {}) {
options = {
Expand Down Expand Up @@ -79,8 +81,10 @@ exports.reset = options => {
}

_shallRenderAsMarkdown = options.renderAsMarkdown
_shallHideMetadata = options.hideMetadata
}

exports.renderContent = content => _markdown.render(content)
exports.renderContent = content =>
_markdown.render(_shallHideMetadata ? metadata.hide(content) : metadata.render(content))

exports.shallRenderAsMarkdown = () => _shallRenderAsMarkdown
11 changes: 11 additions & 0 deletions app/lib/main/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ApplicationSettings extends StorageBase {
#MD_FILE_TYPES_KEY = "md-file-types"
#SHOW_TOC_KEY = "show-toc"
#TOC_WIDTH_KEY = "toc-width"
#HIDE_METADATA_KEY = "hide-metadata"

SYSTEM_THEME = common.SYSTEM_THEME
LIGHT_THEME = common.LIGHT_THEME
Expand All @@ -68,6 +69,8 @@ class ApplicationSettings extends StorageBase {
SHOW_TOC_DEFAULT = false
TOC_WIDTH_DEFAULT = null

HIDE_METADATA_DEFAULT = false

get theme() {
return this._loadValue(this.#THEME_KEY, electron.nativeTheme.themeSource)
}
Expand Down Expand Up @@ -138,6 +141,14 @@ class ApplicationSettings extends StorageBase {
this._storeValue(this.#TOC_WIDTH_KEY, value)
}

get hideMetadata() {
return this._loadValue(this.#HIDE_METADATA_KEY, this.HIDE_METADATA_DEFAULT)
}

set hideMetadata(value) {
this._storeValue(this.#HIDE_METADATA_KEY, value)
}

_loadValue(key, defaultValue) {
return this._data[key] ?? defaultValue
}
Expand Down
24 changes: 24 additions & 0 deletions app/lib/renderer/metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
exports.render = content => {
const lines = content.split("\n")
if (lines.length === 0 || lines[0].trim() !== "---") {
return content
}
lines[0] = "**Metadata**<br>\n```yaml"

const metadataEndIndex = lines.slice(1).findIndex(line => ["---", "..."].includes(line.trim()))
if (metadataEndIndex < 0) {
return content
}
lines[metadataEndIndex + 1] = "```"

return lines.join("\n")
}

exports.hide = content => {
const lines = content.split("\n")
if (lines.length === 0 || lines[0].trim() !== "---") {
return content
}
const metadataEndIndex = lines.slice(1).findIndex(line => ["---", "..."].includes(line.trim()))
return metadataEndIndex >= 0 ? lines.slice(metadataEndIndex + 2).join("\n") : content
}
4 changes: 3 additions & 1 deletion app/lib/toc/tocRenderer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const common = require("../common")
const ipc = require("../ipc/ipcRenderer")
const metadata = require("../renderer/metadata")

const shared = require("./tocShared")

Expand Down Expand Up @@ -286,7 +287,8 @@ exports.reset = reset
exports.addHeader = (title, id) => _headers.push({ title, id })

exports.build = content => {
const lines = content
const lines = metadata
.hide(content)
.split(/\r?\n/)
.map(line => line.trim())
.filter(line => Boolean(line))
Expand Down
8 changes: 8 additions & 0 deletions app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,14 @@ function createMainMenu() {
documentRendering.switchEnableEmojis()
},
},
{
label: "Hide &Metatdata Header",
type: "checkbox",
id: documentRendering.HIDE_METADATA_MENU_ID,
click() {
documentRendering.hideMetadata()
},
},
],
},
{ type: "separator" },
Expand Down
28 changes: 28 additions & 0 deletions test/documents/metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
# Just a random workflow to have a valid YAML

name: Test

on:
workflow_dispatch:
push:
pull_request:

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@master
with:
node-version: latest
- name: Install dependencies
run: npm install
- name: Test
run: npm run test-ci
---

# A file with metadata

This file has a metadata header.

0 comments on commit c9f108b

Please sign in to comment.