Improve mapping slug to filepath, to reduce error when build up graph

(WIP)
This commit is contained in:
Tuan Cao 2022-04-16 22:55:20 +07:00
parent fed481d2a9
commit 99dbccef0a
4 changed files with 73 additions and 82 deletions

View File

@ -23,8 +23,7 @@ export const Node = {
results.push(file);
}
});
const markdownFiles = results.filter(f => f.endsWith(".md"))
return markdownFiles
return results.filter(f => f.endsWith(".md"))
},
readFileSync:function(fullPath){
return fs.readFileSync(fullPath, "utf8")

View File

@ -9,8 +9,8 @@ import frontmatter from "remark-frontmatter";
import externalLinks from "remark-external-links";
import highlight from "remark-highlight.js";
import {Node} from "./node";
const postsDirectory = path.join(process.cwd(), 'posts')
import {getAllFilePaths, toSlug} from "./utils";
path.join(process.cwd(), 'posts');
const isFile = fileName => {
return fs.lstatSync(fileName).isFile()
}
@ -36,7 +36,27 @@ export const Transformer = {
return {}
},
getHtmlContent: function (content, {fileNames}) {
pageResolver: function (pageName) {
const allFileNames = getAllFilePaths()
const result = allFileNames.find(aFile => {
let parseFileNameFromPath = Transformer.parseFileNameFromPath(aFile);
return parseFileNameFromPath === pageName
}
)
// permalink = permalink.replace("ç", "c").replace("ı", "i").replace("ş", "s")
// console.log(`/note/${toSlug(result)}`)
if (result === undefined || result.length === 0) {
console.log("Cannot resolve file path " + pageName)
}
return (result !== undefined && result.length > 0) ? [toSlug(result)] : ["/"]
},
hrefTemplate: function (permalink) {
permalink = Transformer.normalizeFileName(permalink)
permalink = permalink.replace("ç", "c").replace("ı", "i").replace("ş", "s")
return `/note/${permalink}`;
}, getHtmlContent: function (content) {
let htmlContent = []
const sanitizedContent = Transformer.preprocessThreeDashes(content)
@ -48,12 +68,10 @@ export const Transformer = {
.use(wikiLinkPlugin, {
permalinks: null,
pageResolver: function (pageName) {
return [Transformer.parseFileNameFromPath(pageName)]
return Transformer.pageResolver(pageName)
},
hrefTemplate: function (permalink) {
permalink = Transformer.normalizeFileName(permalink)
permalink = permalink.replace("ç", "c").replace("ı", "i").replace("ş", "s")
return `/note/${permalink}`
return Transformer.hrefTemplate(permalink);
},
aliasDivider: "|"
@ -62,6 +80,9 @@ export const Transformer = {
.process(sanitizedContent,
function (err, file) {
htmlContent.push(String(file).replace("\n", ""))
if (err) {
console.log("ERRROR:" + err)
}
}
)
htmlContent = htmlContent.join("")
@ -106,8 +127,7 @@ export const Transformer = {
/* Parse file name from path then sanitize it */
parseFileNameFromPath: function (filepath) {
const parsedFileFromPath = filepath.split("/")[filepath.split("/").length - 1]
const parsedFileName = parsedFileFromPath.replace(".md", "")
return Transformer.normalizeFileName(parsedFileName)
return parsedFileFromPath.replace(".md", "")
},
/* Pair provided and existing Filenames*/
@ -130,8 +150,8 @@ export const Transformer = {
},
getInternalLinks: function (aFilePath) {
const filePaths = Node.getFiles(postsDirectory);
const currentFilePath = Transformer.pairCurrentFile(aFilePath, filePaths)
// const filePaths = Node.getFiles(postsDirectory);
// const currentFilePath = Transformer.pairCurrentFile(aFilePath, filePaths)
const fileContent = Node.readFileSync(aFilePath);
const internalLinks = []
const sanitizedContent = Transformer.preprocessThreeDashes(fileContent)
@ -141,11 +161,13 @@ export const Transformer = {
pageResolver: function (pageName) {
// let name = [Transformer.parseFileNameFromPath(pageName)];
//TODO: Handle # Link
let slug = Transformer.parseFileNameFromPath(pageName);
if (slug.includes('#')) {
console.log(slug)
}
const canonicalSlug = slug.includes('#') ? slug.split('#')[0] : slug
const canonicalSlug = Transformer.pageResolver(pageName)[0]
const backLink = {
title: canonicalSlug,
@ -153,16 +175,16 @@ export const Transformer = {
shortSummary: canonicalSlug
}
if (canonicalSlug != null && internalLinks.find(aLink => aLink.slug === canonicalSlug ) == null) {
if (canonicalSlug != null && internalLinks.find(aLink => aLink.slug === canonicalSlug) == null) {
internalLinks.push(backLink);
}
return [canonicalSlug]
}
,
hrefTemplate: function (permalink) {
permalink = Transformer.normalizeFileName(permalink)
permalink = permalink.replace("ç", "c").replace("ı", "i").replace("ş", "s")
return `/note/${permalink}`
return Transformer.hrefTemplate(permalink)
},
aliasDivider: "|"

View File

@ -5,6 +5,10 @@ import unified from "unified";
import markdown from "remark-parse";
import {toString} from 'mdast-util-to-string'
const dirTree = require("directory-tree");
const postsDirectory = path.join(process.cwd(), 'posts')
export function getContent(filename) {
let {currentFilePath} = getFileNames(filename);
//console.log("currentFilePath: ", currentFilePath)
@ -25,11 +29,8 @@ export function getShortSummary(filename) {
}
const dirTree = require("directory-tree");
const postsDirectory = path.join(process.cwd(), 'posts')
export function getAllFileNames() {
return Node.getFiles(postsDirectory).map(f => Transformer.parseFileNameFromPath(f))
export function getAllFilePaths() {
return Node.getFiles(postsDirectory)
}
export function getFileNames(filename) {
@ -49,7 +50,7 @@ export function getFileNames(filename) {
//TODO remove reference to index/sidebar md
filePaths = filePaths.filter(f => !(f.endsWith("sidebar.md") && f.endsWith("index.md")))
//console.log("\tDirectory is scanning to find corresponding filename")
currentFilePath = Transformer.pairCurrentFile(filename, filePaths)
currentFilePath = toFilePath(filename)
//console.log("\tScan is finished. Founded filepath", currentFilePath, "\n")
}
return {fileNames, currentFilePath};
@ -66,9 +67,7 @@ export function getSinglePost(filename) {
const currentFileFrontMatter = Transformer.getFrontMatterData(fileContent)
const [htmlContent] = Transformer.getHtmlContent(fileContent, {
fileNames: fileNames,
})
const [htmlContent] = Transformer.getHtmlContent(fileContent)
//console.log("hrmlcontents and backlinks")
return {
id: filename,
@ -78,6 +77,29 @@ export function getSinglePost(filename) {
}
export function toFilePath(slug) {
// Construct file name from slug of /notes/abcxyz
return postsDirectory + slug
.replaceAll('__','/')
.replaceAll('--',' ')
.replaceAll('ambersand','&')
+ ".md"
}
export function toSlug(filePath) {
// Convert File Path to unique slug
// let result = filePath.replace('/','__').replace(' ','-').replace('.md', '')
return filePath.replace(postsDirectory, '')
.replaceAll('/','__')
.replaceAll(' ','--')
.replaceAll('&','ambersand')
.replace('.md', '')
}
export function constructBackLinks() {
const filePaths = getContentPaths()
@ -85,7 +107,7 @@ export function constructBackLinks() {
const nodes = []
filePaths.forEach(filename => {
const {currentFilePath, fileNames} = getFileNames(filename)
const {currentFilePath} = getFileNames(filename)
const internalLinks = Transformer.getInternalLinks(currentFilePath)
internalLinks.forEach(aLink => {
@ -97,7 +119,7 @@ export function constructBackLinks() {
}
edges.push(anEdge)
if (nodes.findIndex(aNode => aNode.slug === aLink.slug) === -1) {
aLink.shortSummary = getShortSummary(aLink.slug)
// aLink.shortSummary = getShortSummary(aLink.slug)
console.log(aLink.shortSummary)
nodes.push(aLink)
}
@ -166,7 +188,7 @@ export function getContentPaths() {
//console.log("\n\nAll Posts are scanning")
// Get file names under /posts
const filePaths = Node.getFiles(postsDirectory).filter(f => !(f.endsWith("index") || f.endsWith("sidebar")))
return filePaths.map(f => Transformer.parseFileNameFromPath(f))
return filePaths.map(f => toSlug(f))
}
export function getDirectoryData() {

View File

@ -1,52 +0,0 @@
## About this site
Welcome to Obsidian's official help vault! This page contains a quick index to the things that you may be interested in.
See public hosted version: https://publish.obsidian.md/help
If you found mistakes or missing information, you can contribute to this site here: https://github.com/obsidianmd/obsidian-docs/
## Other languages
This help site is also available in other languages:
- [Italiano](https://publish.obsidian.md/help-it)
- [中文](https://publish.obsidian.md/help-zh)
- [日本語](https://publish.obsidian.md/help-ja)
- [Dansk](https://publish.obsidian.md/help-da)
- [Русский](https://publish.obsidian.md/help-ru)
- [Tiếng Việt](https://publish.obsidian.md/help-vi)
## Credits
[[Credits|Our credits page]] lists all the amazing people who made Obsidian possible.
To read more about the makers, see the [about page](https://obsidian.md/about) on our official website.
## Obsidian
### About the app
- See [[Obsidian|our philosophy for the app]].
### Getting started
- Looking for some functionality? Try the [[command palette]], which contains all available actions you need to work with Obsidian. Just press `Ctrl/Cmd-P` and start typing.
- Learn our flavor of Markdown with [[Format your notes|how to format your notes]].
- Check out some tips on [[Create notes|creating new notes]].
- Check out some tips on [[Internal link|creating internal links]].
- Learn how to [[Embed files|embed other notes or attachments]].
- See the [[Use hotkeys|default keyboard shortcuts]].
### Official plugins
![[Core plugins#Current list of official plugins]]
### Advanced guides
- [[Working with tags]]
- [[Working with backlinks]]
- [[Working with multiple notes]]
- [[Pane layout]]
- [[Working with multiple vaults]]
- [[Working with multiple cursors]]
- [[Using obsidian URI]]