Skip to content

Commit

Permalink
fix: failed to save page in windows by removing invisible characters …
Browse files Browse the repository at this point in the history
…in the page name
  • Loading branch information
sywhb committed Jun 22, 2023
1 parent 3128a10 commit 7a92a9f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"lodash": "^4.17.21",
"luxon": "^3.2.1",
"markdown-escape": "^1.1.0",
"mustache": "^4.2.0"
"mustache": "^4.2.0",
"out-of-character": "^1.2.1"
},
"devDependencies": {
"@semantic-release/changelog": "^6.0.1",
Expand Down
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
isBlockPropertiesChanged,
parseBlockProperties,
parseDateTime,
replaceIllegalChars,
} from './util'

const isValidCurrentGraph = async (): Promise<boolean> => {
Expand Down Expand Up @@ -250,10 +251,8 @@ const fetchOmnivore = async (inBackground = false) => {
for (const article of articles) {
if (!isSinglePage) {
// create a new page for each article
pageName = renderPageName(
article,
pageNameTemplate,
preferredDateFormat
pageName = replaceIllegalChars(
renderPageName(article, pageNameTemplate, preferredDateFormat)
)
targetBlockId = (await getOmnivoreBlock(pageName, blockTitle)).uuid
}
Expand Down
14 changes: 14 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { format } from 'date-fns'
import { diff_match_patch } from 'diff-match-patch'
import { DateTime } from 'luxon'
import escape from 'markdown-escape'
import outOfCharacter from 'out-of-character'
import { Highlight } from './api'

export interface HighlightPoint {
Expand All @@ -11,6 +12,11 @@ export interface HighlightPoint {

export const DATE_FORMAT_W_OUT_SECONDS = "yyyy-MM-dd'T'HH:mm"
export const DATE_FORMAT = `${DATE_FORMAT_W_OUT_SECONDS}:ss`
export const REPLACEMENT_CHAR = '-'
// On Unix-like systems / is reserved and <>:"/\|?* as well as non-printable characters \u0000-\u001F on Windows
// credit: https://github.com/sindresorhus/filename-reserved-regex
// eslint-disable-next-line no-control-regex
export const ILLEGAL_CHAR_REGEX = /[<>:"/\\|?*\u0000-\u001F]/g

export const getHighlightLocation = (patch: string | null): number => {
if (!patch) {
Expand Down Expand Up @@ -172,3 +178,11 @@ export const isBlockPropertiesChanged = (
export const escapeQuotes = (str: string): string => {
return str.replace(/"/g, '\\"')
}

const removeInvisibleChars = (str: string): string => {
return outOfCharacter.replace(str)
}

export const replaceIllegalChars = (str: string): string => {
return removeInvisibleChars(str.replace(ILLEGAL_CHAR_REGEX, REPLACEMENT_CHAR))
}
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,11 @@ color-support@^1.1.3:
resolved "https://registry.npmjs.org/color-support/-/color-support-1.1.3.tgz"
integrity sha512-qiBjkpbMLO/HL68y+lh4q0/O1MZFj2RX6X/KmMa3+gJD3z+WwI1ZzDHysvqHGS3mP6mznPckpXmw1nI9cJjyRg==

[email protected]:
version "1.2.2"
resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.2.2.tgz#cbcc79d5e99caea2dbf10eb3a26fd8b3e6acfa94"
integrity sha512-MKGMzyfeuutC/ZJ1cba9NqcNpfeqMUcYmyF1ZFY6/Cn7CNSAKx6a+s48sqLqyAiZuaP2TcqMhoo+dlwFnVxT9w==

columnify@^1.6.0:
version "1.6.0"
resolved "https://registry.npmjs.org/columnify/-/columnify-1.6.0.tgz"
Expand Down Expand Up @@ -2639,6 +2644,18 @@ glob-parent@^6.0.1:
dependencies:
is-glob "^4.0.3"

[email protected]:
version "7.1.7"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.7.tgz#3b193e9233f01d42d0b3f78294bbeeb418f94a90"
integrity sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ==
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
inherits "2"
minimatch "^3.0.4"
once "^1.3.0"
path-is-absolute "^1.0.0"

[email protected]:
version "7.2.0"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.0.tgz#d15535af7732e02e948f4c41628bd910293f6023"
Expand Down Expand Up @@ -4077,6 +4094,14 @@ ordered-binary@^1.2.4:
resolved "https://registry.npmjs.org/ordered-binary/-/ordered-binary-1.2.5.tgz"
integrity sha512-djRmZoEpOGvIRW7ufsCDHtvcUa18UC9TxnPbHhSVFZHsoyg0dtut1bWtBZ/fmxdPN62oWXrV6adM7NoWU+CneA==

out-of-character@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/out-of-character/-/out-of-character-1.2.1.tgz#383bf683e58f654ee1f348029367edf5c79ba87d"
integrity sha512-clqCX4lNQa5Qr7urMNej2DbKVt+SO5CZ9Sq4XYEJ04qzvDHA4yFMhz0F+vLrfxO89640x8W56D7wtNoAgCCcAA==
dependencies:
colorette "1.2.2"
glob "7.1.7"

p-each-series@^2.1.0:
version "2.2.0"
resolved "https://registry.npmjs.org/p-each-series/-/p-each-series-2.2.0.tgz"
Expand Down

0 comments on commit 7a92a9f

Please sign in to comment.