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); results.push(file);
} }
}); });
const markdownFiles = results.filter(f => f.endsWith(".md")) return results.filter(f => f.endsWith(".md"))
return markdownFiles
}, },
readFileSync:function(fullPath){ readFileSync:function(fullPath){
return fs.readFileSync(fullPath, "utf8") return fs.readFileSync(fullPath, "utf8")

View File

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

View File

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